【问题标题】:“StaleElementReferenceException” in Selenium for a List<WebElement>列表<WebElement> 的 Selenium 中的“StaleElementReferenceException”
【发布时间】:2018-02-06 18:43:26
【问题描述】:

请参考以下代码,此代码将从 findtable 方法中获取所有 orderID 并将所有 orderID 传递给 clickonIndividualOrderID 方法 所以光标移动到每个orderid并点击它,会出现一个新页面,它会获取状态并点击完成,如果我们尝试选择下一个orderID,它会返回旧页面,它会抛出异常

您能否建议一些解决此问题的方法 提前致谢

List<WebElement> orderID = new ArrayList<WebElement>();
List<WebElement> statusID = new ArrayList<WebElement>();

public void OrderandReleases()     
{
orderID = outboxpage.findtable(orderID);
util.pause("1");
statusID = outboxpage.findordernumber(statusID, orderID);
}

public List<WebElement> findOrderID(List<WebElement> orderID) {
WebElement table = driver.findElement(By.id("_kod7c3"));      
List<WebElement> allRows = table.findElements(By.tagName("tr"));

//And iterate over them, getting the cells 
for (int i = 1; i < allRows.size(); i++) {
 List<WebElement> alltd = allRows.get(i).findElements(By.tagName("td"));
                for (int j = 0; j < alltd.size(); j++) {
                    if (j == 1) {
                        orderID.add(alltd.get(j));
                        continue;
                    }
                }
            }
              return orderID;
}

public List<WebElement> clickonIndividualOrderID(List<WebElement> 
statusID,List<WebElement> orderID){
    for (int i = 0; i < orderID.size(); i++) {  
    WebElement table = driver.findElement(By.id("_kod7c3")); 
        if (table.isDisplayed()) {
            System.out.println("Clicking on 
order="+orderID.get(i).getText()); -> //first time it will run fine , second time when it loops back it will thow the execption StaleElementReferenceException here
            orderID.get(i).click(); -> //it is clicking on a order link and it will take me to next page
            driver.findElement(By.id("_jxndro")).click();

            WebElement table2 = driver.findElement(By.xpath("//*
[@id=\"_mfb\"]"));  
            List<WebElement> allRows2 = 
table2.findElements(By.tagName("tr"));

            String col = "";
            for (int j = 1; j < allRows2.size(); j++) {
                List<WebElement> alltd2 = 
allRows2.get(j).findElements(By.tagName("td"));
                int flag = 0;
                for (int k = 0; k < alltd2.size(); k++) {
                    col = alltd2.get(k).getText().toString().trim();
                    System.out.println(col);
                    if (col.equals("Failed")||col.contains("FE-")) {
                        statusID.add(alltd2.get(++k));
                        driver.findElement(By.id("_uvsub")).click(); --> // it will take me back to the first page
                        flag =1;
                        break;
                    }
                }
                 if(flag==1)
                        break;
            }
        }
    }
    return statusID;
}

【问题讨论】:

  • 帮助我们! 缩写您的代码以仅显示相关部分,进一步描述页面(如果可能,添加其代码,或附上其 URL),通常只需给出我们的工具可以提供帮助!
  • codeSystem.out.println("点击订单="+orderID.get(i).getText()); //第一次它会运行良好,第二次当它循环回来时,它将在这里执行 StaleElementReferenceException codeorderID.get(i).click(); //它正在点击一个订单链接,它会带我到下一页
  • 更多代码:code driver.findElement(By.id("_jxndro")).click(); codeWebElement table2 = driver.findElement(By.xpath("//* [@id=\"_mfb\"]")); code List allRows2 = table2.findElements(By.tagName("tr"));如果您需要更多信息,请告诉我,提前谢谢

标签: java selenium page-factory staleelementreferenceexception


【解决方案1】:

当您从任何第三方代码中发现特定异常时,您应该在该代码的官方文档中查找信息(如果有)。您可以找到有关“StaleElementReferenceException”here

的信息

在上述页面中,您会找到这个

最常见的原因是元素所在的页面已刷新,或者用户已导航到另一个页面。

您正在导航到另一个页面,因此所有引用都将丢失。驱动程序不知道它是相同的页面,具有相同的对象。

您需要寻找一种不同的方式来跟踪您已单击的链接,或者在新选项卡/窗口中打开链接,以执行您需要的任何操作,然后处理选项卡/窗口而不是返回导航.

【讨论】:

  • 感谢更新,一次查询,在此代码codeorderID.get(i).click();当它点击它时,它会将我们带到另一个页面,所以我如何在这个进入新窗口/标签的代码中实现它。请帮助我
【解决方案2】:

StaleElementReferenceException 的官方文档说:

表示对元素的引用现在“过时” --- 该元素不再出现在页面的 DOM 上。

如果您在页面之间来回导航,就像您正在做的那样,那么这是预期的行为。

解决此问题的常规方法是在循环外跟踪WebElements,而是在每次迭代期间在循环内找到它们。像这样的:

// this will not change, but you need to adjust for your case!
By pageLocator = By.tagName("a");
int pageCount = driver.findElements(pageLocator).size();

for (int i = 0; i < pageCount; i++) {
    WebElement pageLink = driver.findElements(pageLocator).get(i);
    pageLink.click();
    // at this point pageLink is gone stale!

    // do some stuff

    driver.navigate().back();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2020-09-22
    • 2015-01-16
    • 2011-06-18
    相关资源
    最近更新 更多