Selenium中的List< WebElement>中的"StaleElementReferenceException". [英] “StaleElementReferenceException” in Selenium for a List<WebElement>

查看:381
本文介绍了Selenium中的List< WebElement>中的"StaleElementReferenceException".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考以下代码,该代码将从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的官方文档说:

指示对元素的引用现在已陈旧" ---该元素不再出现在页面的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();
}

Please refer the below code, this code will fetch all the orderID from findtable method and it passes all of the orderID to clickonIndividualOrderID method so the cursor moves to each orderid and it clicks on it, a new page will come and it fetch the status and clicks on done and it comes back to old page now if we try to select next orderID, it will throw the exeception

Could you please suggest some approaches to resolve this issue Thanks in advance

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;
}

解决方案

The official documentation for StaleElementReferenceException says:

Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page.

If you are navigating back and forth between pages, as you are doing, then this is the expected behaviour.

The normal way to approach this is to not keep track of the WebElements outside of the loop, but find them inside the loop during each iteration. Something like this:

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

这篇关于Selenium中的List&lt; WebElement&gt;中的"StaleElementReferenceException".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆