微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当在行中执行某些操作时重新加载页面后表中的行数发生更改时,如何在 selenium java 中处理 webtable

如何解决当在行中执行某些操作时重新加载页面后表中的行数发生更改时,如何在 selenium java 中处理 webtable

目前我正在经历一个情况。表的主体中有三行。如果行与文本匹配,我必须对每一行执行一些操作。为此,我获取行的大小并使用 for 循环并检查条件。当条件满足时,我必须执行一些操作,根据我的期望,该行将从 webtable 中删除。此外,我收到 org.openqa.selenium.StaleElementReferenceException: here "totalOrders.get(i).click();"当循环尝试在下一行执行操作时

这是我的代码的一部分:

By loading = By.xpath("//div[@class='loading-wrap']");
By orders = By.xpath("//tbody/tr"); //this retruns 3 row

public void invoiceAllStockOrder() {

    eu.waitForInvisibilityOfElementLocated(loading,10);

    List<WebElement> totalOrders = eu.getElements(orders);
    int rowSize =totalOrders.size();
    if(rowSize == 0) {
        System.out.println("No order");
    }

    else {

        for (int i = 0; i < totalOrders.size(); i++) {
            eu.waitForInvisibilityOfElementLocated(loading,10);

            totalOrders.get(i).click();//getting stale element exception here when i = 1 but there are still 2 rows left
            selectInstockOrders();
            invoiceOrder();
                
        }
    }
}

解决方法

If the page has Javascript which automatically updates the DOM,you should assume a StaleElementException will occur.

Can you try with the below code,I hope this will work for you

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementException e) {
        }
        attempts++;
    }
    return result;
}
This will attempt to find and click the element. If the DOM changes 
between the find and click,it will try again.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?