【问题标题】:For loop for Checking list of items in Selenium WebDriverFor 循环检查 Selenium WebDriver 中的项目列表
【发布时间】:2015-05-12 13:17:18
【问题描述】:

我已经为检查列表 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();
}

【问题讨论】:

  • 错误是什么? StaleElement 引用异常?
  • @Saifur: 错误就像,org.openqa.selenium.StaleElementReferenceException: Element not found in the cache...
  • 是的。看答案。这就是我的怀疑

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

@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(); 
} 

所以,上面我只是稍微调整了您的代码,并在相关的 cmets 中进行了更改以及更改原因。希望这对你有用。 :)

【讨论】:

  • 这对我的解决方案来说就像一个魅力,它围绕着网格上的一个按钮,每行都有一个动态 ID。
【解决方案2】:

这样做的可能问题是 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();
}

【讨论】:

  • 嗨 Saifur,非常感谢您的即时回放,您能否告诉我您添加的以下代码的更多信息。我是硒的新手,也在开发中,所以这对你来说可能是一个简单的问题吗? WebElement myDynamicElement = (new wd(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(by));
  • @Santosh 该代码之前直接使用了By 选择器。我正在使用一种称为显式等待的等待机制。它等待元素加载到页面中。阅读here
  • 它仍然没有循环到列表末尾。请协助。 By byxpath = By.xpath("//*[contains(@id,'dealView')]//img");列表 listofItems = wd.findElements(byxpath); for(int i=1; i
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
相关资源
最近更新 更多