遍历WebElement列表时获取StaleElementReferenceException

时间:2019-08-01 15:42:07

标签: selenium selenium-webdriver staleelementreferenceexception

我正在尝试使以下情况自动化:

  • 转到amazon.com
  • 搜索耳机
  • 将第一个结果页中的所有畅销书添加到购物车

为这种情况编写脚本的步骤如下:

  • 转到amazon.com
  • 在搜索字段中输入文本“耳机”
  • 点击搜索按钮
  • 点击标记为“畅销书”的链接
  • 点击“添加到购物车”按钮
  • 导航回到结果页面
  • 点击另一个标记为“畅销书”的链接
  • 点击“添加到购物车”按钮
  • 导航回到结果页面

所有畅销书具有相同的xpath:

//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span

因此,我已经实现了以下WebElement列表:

List<WebElement> bestsellers = driver.findElements(By.xpath("xpath of bestsellers"));

我已经实现了单击链接并使用循环的3种方式将其添加到购物车,如下所示:

for(WebElement product: bestsellers) {
    product.click();
    clickOnAddToCartButton();
    driver.navigate().back();
}



for(int i=0; i<bestsellers.size(); i++) {
        System.out.println(bestsellers.size());
        bestsellers.get(i).click();
        clickOnAddToCartButton();
        driver.navigate().back();

    }



Iterator<WebElement> i = bestsellers.iterator();
    while(i.hasNext()) {
        WebElement product = i.next();
        wait.until(ExpectedConditions.elementToBeClickable(product));

        product.click();
        clickOnAddToCartButton();
        driver.navigate().back();
    }

运行脚本时,“畅销书”列表中包含3个元素。执行循环时,将单击第一个元素并将其添加到购物车中,驱动程序将导航回到结果页面。然后,使用上述3种方式获取staleElementReferenceException。

更新: 我已经实现了以下方案:

for(int i=0; i<bestsellers.size(); i++) {

        System.out.println("Current :" + i);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span")));
        driver.findElements(By.xpath(".//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span")).get(i).click();
        clickOnAddToCartButton();
        //clickOnViewCart();
        try {
            wait.until(ExpectedConditions.elementToBeClickable(cartButton));
        }catch(TimeoutException e) {
            wait.until(ExpectedConditions.elementToBeClickable(viewCartButton));
        }
        if(i==(bestsellers.size()-1)) {
            try {
                wait.until(ExpectedConditions.elementToBeClickable(cartButton));    
                cartButton.click();
                break;
            }catch(TimeoutException e) {
                wait.until(ExpectedConditions.elementToBeClickable(viewCartButton));    
                viewCartButton.click();
                break;
            }
        }

        driver.navigate().back();

1 个答案:

答案 0 :(得分:1)

在浏览器中单击元素或back()时,元素引用将在硒中更新,因此您无法指向具有旧引用并导致StatleElementException的元素。

当您必须遍历多个元素的交互时,请考虑使用此方法。

List<WebElement> bestsellers = driver.findElements(By.xpath("xpath of bestsellers"));
for(int i=0; i<bestsellers.size(); i++) {
    System.out.println("Current Seller " + i);
    // here you are getting the elements each time you iterate, which will get the
    // latest element references
    driver.findElements(By.xpath("xpath of bestsellers")).get(i).click();
    clickOnAddToCartButton();
    driver.navigate().back();

}