【问题标题】:Selenium Java Chrome: Stale exceptionSelenium Java Chrome:过时的异常
【发布时间】:2017-05-04 15:55:24
【问题描述】:

这是一个代码片段:

    System.setProperty(Constants.WEBDRIVER_CHROME_DRIVER_PROP, Constants.WEBDRIVER_CHROME_DRIVER_PATH);

    m_chromeWebdriver = new ChromeDriver();
    m_chromeWebdriver.get("mysite.org");

    WebElement arrowElement = m_chromeWebdriver.findElement(By.cssSelector(_ARROW_NEXT_DAY));
    arrowElement.click();

    WebElement elmMainTable = m_chromeWebdriver.findElement(By.className("table-main"));

    List<WebElement> allRows = elmMainTable.findElements(By.tagName("tr"));

    for (WebElement row : allRows) {
        
        List<WebElement> cells = row.findElements(By.tagName("td"));
        for (WebElement cell : cells) {
            System.out.println(cell.getText());
        }
    }
    m_chromeWebdriver.quit();

在最后一行我得到一个

"过时的元素引用:元素未附加到页面 文件”

异常。

为什么以及如何解决这个问题?
我使用 Chromdriver 2.2.9。

【问题讨论】:

  • 您正在通过循环迭代列表元素。对吗?
  • elmNextDayArrow.click(); 这行是做什么的?
  • @NarendraRajput 我想...
  • 请修正您的代码并提供minimal reproducible example,以便我们为您提供帮助。
  • @kushal。 - 单击元素...这不太重要...它有效。问题出在最后一行。

标签: java selenium selenium-chromedriver


【解决方案1】:

好吧,因为这可能不是完美的解决方案 - 至少它对我有用...

我把所有相关的代码都放到了这样一个方法中:

private static void handleTable() {

    for (int i = 1; i < 5; i++) {

    try {
        WebElement elmMainTable = m_chromeWebdriver.findElement(By.className("table-main"));

        List<WebElement> allRows = elmMainTable.findElements(By.cssSelector(".table-main tr"));

        for (WebElement row : allRows) {

            List<WebElement> cells = row.findElements(By.tagName("td"));

            for (WebElement cell : cells) {

                System.out.print(cell.getText() + "\t");
            }
            System.out.println();
        }

    } catch (StaleElementReferenceException e) {

        //e.printStackTrace();

        handleTable();
    }

    return;
}

}

它奏效了!当然,您可以将值 5 更改为任意值。

【讨论】:

    【解决方案2】:

    我了解到 Selenium 陈旧异常的主要原因是 DOM 中的元素更改。如果过时异常是由您未处理的 Web 元素引起的,您可以尝试下面的 JAVA 中的 try-catch 和 continue 方法。

    //get webelement list of button on the OU tab
    List<WebElement> ouList = 
    Logon.wDriver.findElements(By.tagName(PagePropertise.tagButton)); 
    // it should be 6
    System.out.println("List Size is: "+ouList.size()); 
    
    for (int i=0; i<ouList.size(); i++ ) {
       //get each button element to verify with input Org name
        WebElement ouName= ouList.get(i); 
        String ouLabel = null;
        try {
            ouLabel = ouName.getText();
        } catch (StaleElementReferenceException e) {
            //handle in exception catch, just skip invalid element and continue for to handle rest of loop
            System.out.println("WebElement "+i+" = text "+ ouLabel);
            continue;
        }
        System.out.println("WebElement "+i+" = text "+ ouLabel);
    
        //webelement processing
        ...
        //for some reason some of webelements in the OUlist were changed for above processing code.
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2021-01-04
      • 2021-02-21
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多