我对自动化测试完全陌生。在参考了一些教程之后,我创建了一个自动测试用例。我尝试自动化的测试用例是在单击表的一个标题后检查排序是否正常工作。
我的自动测试用例失败,出现以下异常:
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"));
有人能告诉我isse的原因以及如何解决吗?
非常感谢您的帮助
使用webDriveretc
并使驱动程序等待直到元素位于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");
我希望这有帮助。
嗯,StaleElement异常可能真的很令人沮丧...特别是在使用chrome驱动程序时...但是到目前为止,唯一的解决方法就是简单地使用我所说的原始等待:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
虽然纯粹主义者可能会说我们在使用神奇的数字,但一旦你知道了安全的等待幅度,那么我就知道它在95%以上的情况下是有效的。
StaleElementException告诉您,您在客户端代码中引用的DOM元素(通过findElement)已经不存在了。在您的情况下,行1元素不再存在。这当然是一个时间问题,因为您的点击排序就在上面,并且不需要注意确保排序已经完成。如果不能更深入地了解排序是如何实现的,或者需要多长时间,这将是我最好的猜测。如果在遍历DOM对象时排序结束,这些对象将在DOM中重新排列,并在客户端代码中丢失
线程“main”org.openqa.selenium.StaleElementReferenceException中的异常:stale的元素引用:该元素不再附加到DOM或页面已刷新
我在我的selenium java项目中不断收到一条错误消息。
我正在尝试访问一系列的网页和递归。访问一个页面,然后返回,然后移动到下一个页面,依此类推。用我的代码,我可以访问第一页,然后返回到上一页,但我不能访问下一页,并得到一个陈旧的元素引用错误。我通读了关于这个错误的答案,但仍然不知道我的代码在哪里更改了状态... 下面是我的代码
我有一个网页,其中包含一些JavaScript并执行一些Ajax调用。当尝试使用Selenium测试它时,我随机得到“PHPUnit_Extensions_Selenium2TestCase_WebDriverException:Element不再附加到DOM”消息,可能是5次运行中的一次。 现在我意识到了Ajax调用和测试引擎之间的竞争问题,我已经采取了措施来保护它,但仍然存在一些问题。我的场景
我必须打开页面中的所有链接,然后检查是否有文本(“无信息”)。 我用了赛琳娜v3。5和硒v2。53个图书馆。然而,我有一个例外“StaleElementReferenceException”。 我的代码如下:
问题内容: 我试图了解为什么在尝试显示从网站上获取的项目时会出现错误。我也在浏览器中使用google chrome。 还有我代码的其他部分 每当我使用“ chromeDriver.FindElement(By.Id(“ something.link.text”))。Click();“ ,它给出一个错误。我无法显示提取的数据。 在错误消息中,它说“ OpenQA.Selenium.StaleElem