selenium - 遍历 WebElement 列表时获取 StaleElementReferenceException
问题描述
我试图自动化以下场景:
- 访问 amazon.com
- 搜索耳机
- 将第一个结果页面中的所有畅销书添加到购物车
我为这个场景编写脚本所遵循的步骤:
- 访问 amazon.com
- 在搜索字段中输入文本“耳机”
- 点击搜索按钮
- 点击标记为“畅销书”的链接
- 点击“加入购物车”按钮
- 导航回结果页面
- 点击另一个标记为“畅销书”的链接
- 点击“加入购物车”按钮
- 导航回结果页面
所有畅销书都有相同的 xpath:
//span[.='Best Seller']/../../../../../../../../following-sibling::div/div/following-sibling::div/div/div/div/div/div/h2/a/span
所以我实现了一个 WebElements 列表,如下所示:
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();
解决方案
当您在浏览器中单击元素或 back() 时,元素引用将在 selenium 中更新,因此您不能指向具有旧引用的元素并导致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();
}
推荐阅读
- azure - 如何拒绝订阅级别贡献者角色对 Azure 中特定资源组的访问
- perl - 使用 PERL 将 KB、MB、GB、TB 转换为 KB
- delphi - 无法从 Indy TIdCookieManager 获取 cookie
- swift - 斯威夫特:手势不会带回 TabBar
- javascript - taxfield 动态锁定,但 tab 键可以到达和编辑文本字段.. 我该如何修复它?
- birt - Birt 报告 - 在 Excel 中显示的隐藏交叉表单元格
- javascript - Javascript如何连接两个具有相同属性值的数组?
- mobile - Flutter 换屏回调
- sql - 如何使用多列取消透视
- c - 算法图深度 - 首次搜索