gpt4 book ai didi

selenium - StaleElementReferenceException : Element is no longer attached to the DOM: Selenium

转载 作者:行者123 更新时间:2023-12-02 21:43:55 28 4
gpt4 key购买 nike

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

我的自动化测试用例因以下异常而失败:

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"));

有人可以告诉我这个问题的原因以及如何解决吗?

感谢任何帮助

最佳答案

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

关于selenium - StaleElementReferenceException : Element is no longer attached to the DOM: Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14136122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com