有一个方案是收集所有div id并逐个循环它们以完成迭代。我已经完成了这个场景,但需要更多时间才能传递所有ID。
您能否建议如何加快速度。
以下是我的代码段。
List<WebElement> listoftab = driver.findElements(by.xpath(".//*[contains (@id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for(int i =1; i<=110;i++){
boolean present;
try {
driver.findElement(By.xpath(".//*[@id='tabZ"+i+"']/div/div[1]"));
present = true;
if(clas1.equalsIgnoreCase(driver.findElement(By.xpath(".//*[@id='tabZ"+i+"']/div/div[1]")).getAttribute("class"))) {
tabloop:
for(int j=1;j<=15;j++) {
if(clas2.equalsIgnoreCase(driver.findElement(By.xpath(".//*[@id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("class"))) {
String ls = driver.findElement(By.xpath(".//*[@id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("id");
System.out.println(ls);
driver.findElement(By.xpath(".//*[@id='"+ls+"']/div[1]/div[2]/canvas[2]")).click();
Thread.sleep(3000);
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
答案 0 :(得分:1)
尝试此代码,因为您多次使用driver.findElement()。尝试避免查找元素而是将它们存储在变量中。
List<WebElement> listoftab = driver.findElements(By
.xpath(".//*[contains (@id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for (int i = 1; i <= 110; i++) {
boolean present;
try {
WebElement element=driver.findElement(By.xpath(".//*[@id='tabZ" + i
+ "']/div/div[1]"));
present = true;
if (clas1.equalsIgnoreCase(element.getAttribute("class"))) {
tabloop: for (int j = 1; j <= 15; j++) {
WebElement element1=driver.findElement(
By.xpath(".//*[@id='tabZ" + i
+ "']/div/div[1]/div[" + j + "]"));
if (clas2.equalsIgnoreCase(element1
.getAttribute("class"))) {
String ls = element1.getAttribute("id");
System.out.println(ls);
driver.findElement(
By.xpath(".//*[@id='" + ls
+ "']/div[1]/div[2]/canvas[2]"))
.click();
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
尽量避免艰难等待。更好地了解fluentwa。