package com.example.demo.test;
import com.sun.deploy.security.SelectableSecurityManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class ElementsClick {
static WebDriver driver = null;
static void init() {
System.setProperty("webdriver.chrome.driver", "D:\\javaenvirement\\webdriver\\chromedriver.exe");
driver = new ChromeDriver();
}
static void getUrl() {
init();
String url2 = "http://www.magicq.fun";
driver.get(url2);
}
static void sleeping() throws InterruptedException {
Thread.sleep(3000);
}
static void reback() {
driver.findElement(By.xpath("//a[@class='link-work']")).click();
}
//循環某班級作業列表
static void findElenens() throws InterruptedException {
sleeping();
List<WebElement> hovs = null;
hovs = driver.findElements(By.xpath("//*[@class='style-mod__cor-btn___14ZVF']"));
for (int i = 0; i < hovs.size(); i++) {
hovs = driver.findElements(By.xpath("//*[@class='style-mod__cor-btn___14ZVF']"));
sleeping();
hovs.get(i).click();
tests();
sleeping();
reback();
sleeping();
}
sleeping();
}
static void sencondView() throws InterruptedException {
sleeping();
WebElement parent_elements = driver.findElement(By.xpath("//*[@class='style-mod__side-main___21OrP']"));
for (int i = 0; i < 3; i++) {
}
}
static void chormeQuit() throws InterruptedException {
sleeping();
driver.quit();
}
//循環作業學生列表
static void tests() throws InterruptedException {
//獲取patent_e的第一級所有的子元素
//子元素的獲取方式不一致,但需要全部獲取同一級別的所有元素,可以使用獲取父類的子類的左右元素
WebElement parent_e = driver.findElement(By.xpath("//*[@class='style-mod__side-main___21OrP']"));
List<WebElement> childrens = null;
childrens= parent_e.findElements(By.xpath("./*"));
sleeping();
/* List<WebElement> elements = driver.findElements(By.xpath("//span[text()='批改']"));
System.out.println(elements.size());
for (int j = 0; j < elements.size(); j++) {
elements.get(j).click();
sleeping();
}*/
for(int i = 0 ; i < childrens.size() ; i++){
childrens= parent_e.findElements(By.xpath("./*"));
childrens.get(i).click();
//進入學生作業批改詳情頁進行一系列批改操作
sleeping();
//點擊批改
List<WebElement> elements = null;
elements = driver.findElements(By.xpath("//*[@class='style-mod__tabs-item___15hhL style-mod__active___220EW']"));
System.out.println(elements.size());
if(elements.size()>0){
for(int j = 0; j < elements.size() ; j++){
elements.get(j).click();
sleeping();
}
}else {
continue;
}
sleeping();
}
}
static void pigai() throws InterruptedException {
List<WebElement> elements = driver.findElements(By.xpath("//*[@class='style-mod__tabs-item___15hhL style-mod__last-item___1GLZk']"));
if (elements.size() > 0) {
for (int j = 0; j < elements.size(); j++) {
elements.get(j).click();
sleeping();
}
}
}
public static void main(String[] args) throws InterruptedException {
getUrl();
findElenens();
chormeQuit();
}
}
Java+selenium自動化測試代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.