Java 用于检查 Selenium WebDriver 中的项目列表的 For 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30192092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For loop for Checking list of items in Selenium WebDriver
提问by Santosh
I have written below code for Checking list Web Elements, but below code is running but for only 1st item its no looping to end of loop.
我已经写了下面的代码来检查列表 Web 元素,但下面的代码正在运行,但只有第一个项目没有循环到循环结束。
List <WebElement> listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
for (int i=1; i<=listofItems.size(); i++)
{
listofItems.get(i).click();
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println(i);
System.out.println("pass");
wd.navigate().back();
}
采纳答案by Subh
@Saifurhas explained nicely regarding the issue. So, I will just put the code that will see you through
@Saifur已经很好地解释了这个问题。所以,我只会放代码让你通过
List <WebElement> listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
WebDriverWait wait = new WebDriverWait(wd, 20); //Wait time of 20 seconds
for (int i=1; i<=listofItems.size(); i++)
{
/*Getting the list of items again so that when the page is
navigated back to, then the list of items will be refreshed
again */
listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
//Waiting for the element to be visible
//Used (i-1) because the list's item start with 0th index, like in an array
wait.until(ExpectedConditions.visibilityOf(listofItems.get(i-1)));
//Clicking on the first element
listofItems.get(i-1).click();
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.print(i + " element clicked\t--");
System.out.println("pass");
wd.navigate().back();
}
So, above I have just tweaked your code a bit and have the relevant comments where changes are made and why. Hope this works out for you. :)
所以,上面我只是稍微调整了您的代码,并在进行更改的地方以及原因进行了相关评论。希望这对你有用。:)
回答by Saifur
The possible issue with this is the DOM refresh. You cannot find a list and click through the elements back and forth and reference to the same list since the DOM has refreshed after first click. The best solution of that problem is to find the element on the fly. Plus, implicit wait is firm for that driver instance once you set that. So, you do not have to set the wait for each element look up. Instead set it where you instantiate the driver.(possibly). However, I think the explicit wait is a best fit here.
可能的问题是 DOM 刷新。您无法找到列表并来回单击元素并引用同一个列表,因为在第一次单击后 DOM 已刷新。该问题的最佳解决方案是动态查找元素。另外,一旦您设置了该驱动程序实例,隐式等待就是固定的。因此,您不必为每个元素查找设置等待。而是将它设置在您实例化驱动程序的地方。(可能)。但是,我认为显式等待最适合这里。
By byXpath = By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img");
List <WebElement> listofItems = wd.findElements(byXpath);
for (int i=1; i<=listofItems.size(); i++)
{
//I would suggest you to see if you can improve the selector though
By by= By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img[" + i + "]");
WebElement myDynamicElement = (new wd(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(by));
System.out.println(i);
myDynamicElement.click();
wd.navigate().back();
}