我已经做了一些非常彻底的挖掘,并且很难确定到底是哪个元素导致了这个异常被抛出。我正在使用Java和Selenium 2,最近接管了一个相当大的测试套件来维护。与其盲目地在失败的代码行中和周围放置fluent等待,我更希望实际确定是哪些元素导致了问题。
我曾经考虑过将一串单行逻辑分解成更多的代码行,以帮助确定它,但是由于这些随机故障是间歇性的,而且到处都是,我正在设法在堆栈跟踪中获得元素或定位符的实际名称。
这是可能的吗?还是我必须对代码端进行大量的重构?
下面是一个堆栈跟踪示例:
Starting ChromeDriver (v2.3) on port 24902
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=30.0.1599.101)
(Driver info: chromedriver=2.3,platform=Mac OS X 10.8.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.33.0', revision: '4ecaf82108b2a6cc6f006aae81961236eba93358', time: '2013-05-22 12:00:17'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.5', java.version: '1.7.0_21'
Session ID: f3e5d2c3eb54afc2fcaacc1c663435e9
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.3}, rotatable=false, locationContextEnabled=true, version=30.0.1599.101, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
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:525)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:187)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByCssSelector(RemoteWebElement.java:240)
at org.openqa.selenium.By$ByCssSelector.findElements(By.java:417)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:163)
at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.rowPropertyIsInCustomSection(CustomSectionScreen.java:54)
at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.editNameForProperty(CustomSectionScreen.java:90)
at com.xmatters.webdriver.tests.REBProperties_BooleanTest.confirmBooleanPropertyNameCanBeEditedOnLayoutTab(REBProperties_BooleanTest.java:526)可疑行上的代码位于此辅助函数中。第54行是包含List<WebElement>...的行
public Integer rowPropertyIsInCustomSection(String propertyName) {
wait.until(ExpectedConditions.presenceOfElementLocated(customSectionLocator));
WebElement customSection = driver.findElement(customSectionLocator);
//wait until properties are present in custom section
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(propertiesInCustomSectionLocator));
List<WebElement> propertiesInCustomSection = customSection.findElements(propertyNameLocatorInCustomSection);
By propertyNameLoc = By.id("propertyName");
for (int i = 0; i < propertiesInCustomSection.size(); i++) {
wait.until(ExpectedConditions.visibilityOfElementLocated(propertyNameLoc));
String propName = propertiesInCustomSection.get(i).findElement(propertyNameLoc).getText();
if (propName.equals(propertyName)) {
return i;
}
}
return null;很抱歉,如果在其他地方已经回答了这个问题,但是我很难在堆栈跟踪中解决这个问题。这段代码都是别人写的,所以现在把它一片片地剖析是我最后的选择。
提前谢谢你的指点。
达尔文。
发布于 2013-10-19 01:36:30
我很确定这是customSection元素。
WebElement#findElements()绝对不会在任何新发现的元素上抛出一个StaleElementReferenceException (这将是一个糟糕的Selenium bug)。
正如在 docs中所说的,它将抛出在先前发现的WebElement上调用的大多数方法。因此,当您尝试对一个元素调用findElements()时,它会执行一个由于某种原因而失败的新鲜度检查。
在搜索customSection之前,尝试等待它的一些属性出现。当向部分添加属性时,元素可能会被丢弃并替换为新的元素。或者您可以尝试所描述的here技术之一,即NeverStaleWebElement。
https://stackoverflow.com/questions/19461303
复制相似问题