繁体   English   中英

Selenium中的“ StaleElementReferenceException”用于列表<WebElement>

[英]“StaleElementReferenceException” in Selenium for a List<WebElement>

请参考下面的代码,该代码将从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;
}

每当您从任何第三方代码中找到特定的例外情况时,都应在该代码的官方文档中查找可用的信息。 您可以在此处找到有关“ StaleElementReferenceException”的信息

在上述页面中,您会发现这个

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

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

您需要寻找另一种方式来跟踪已单击的链接,或者在新的选项卡/窗口中打开链接,以执行所需的任何操作,然后处理该选项卡/窗口,而不是回头浏览。

StaleElementReferenceException的官方文档说:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM