簡體   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