【问题标题】:StaleElementReferenceException: Element is no longer attached to the DOM: SeleniumStaleElementReferenceException:元素不再附加到 DOM:Selenium
【发布时间】:2012-12-17 15:37:10
【问题描述】:

我对自动化测试完全陌生。在参考了一些教程后,我创建了一个自动化测试用例。我尝试自动化的测试用例是在单击表格的一个标题后检查排序是否正确。

我的自动化测试用例失败,出现以下异常:

org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 12 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.35-30-generic',     java.version: '1.6.0_20'
Driver info: driver.version: RemoteWebDriver
Session ID: 95b80ea0-26ac-45e0-a407-79f5b687504a
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:169)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:240)
at org.openqa.selenium.By$ByTagName.findElements(By.java:312)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:151)
at Sorting.sorting_column(Sorting.java:68)

以下是代码:

        //Fetching values from the column of a table
    WebElement table = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows = table.findElements(By.tagName("tr"));

    for (WebElement row : rows) {

        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (!cells.isEmpty() && cells.get(0).isDisplayed()) {
            a = cells.get(0).getText();
        }
        b[i] = a;
        i++;
    }
    //Click on column header to sort   
    driver.findElement(By.cssSelector("html body#Body form#Form div#container.container div#Wrapper div#Main div#Panes div#ContentsContainer.box-shadow div#Contents div#Content div#dnn_ContentPane.MainContent div.DnnModule div.dnnPEMContNotitle div#dnn_ctr381_ContentPane.dnnPEMContNotitleBody div#dnn_ctr381_ModuleContent.DNNModuleContent div#dnn_ctr381_View_dnn_ctr381_View_DealerListPanel div#dnn_ctr381_View_DealerList.RadGrid table#dnn_ctr381_View_DealerList_ctl00.rgMasterTable thead tr th.rgHeader a")).click();
    WebElement table1 = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows1 = table1.findElements(By.tagName("tr"));
    for (WebElement row1 : rows1) {
        List<WebElement> cells1 = row1.findElements(By.tagName("td"));// Exception here
        if (!cells1.isEmpty() && cells1.get(0).isDisplayed()) 
            {

         c = cells1.get(0).getText();

       }

例外来自这一行:

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

谁能告诉我这个问题的原因以及如何解决?

感谢任何帮助

【问题讨论】:

标签: selenium automation automated-tests


【解决方案1】:

StaleElementException 告诉您,您在客户端代码中(通过findElement)引用的 DOM 元素不再存在。在您的情况下,row1 元素不再存在。这肯定是一个时间问题,因为您点击排序就在上面,并且没有注意确保排序已经完成。如果没有更深入地了解排序是如何实现的,或者需要多长时间,这将是我最好的猜测。如果在您遍历 DOM 对象时排序结束,这些将在 DOM 中重新排列并丢失在您的客户端代码中

【讨论】:

  • 有没有办法测试这种情况,或者我能做的就是捕捉那个异常?
【解决方案2】:

好吧,StaleElement 异常确实令人沮丧……尤其是在使用 chrome 驱动程序时……但目前唯一的解决方法就是使用我所谓的 Raw 等待:

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

虽然纯粹主义者可能会说我们使用的是幻数,但一旦你知道安全等待的幅度,我就知道它在 95% 以上的情况下有效。

【讨论】:

    【解决方案3】:

    使用webDriverwait 并让驱动程序等待,直到元素位于 dom 上。

    WebDriverWait wait = new WebDriverWait(driver,20);
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*
    
    [@id='mndf']/form/fieldset/table/thead/tr/th")));
    

    使用输出语句判断元素是否被定位。

    System.out.println("element located");
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      这与 DOM 变化有关,因为当页面上发生交互时,一些 javascript 被执行。因此,由于错误表明元素已过时,因此解决方案是再次执行 driver.findElement 以摆脱它。遇到同样的问题并解决了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2014-06-06
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多