我运行自动化(黄瓜,junit,硒铬网络驱动程序)。
当我在本地运行时,测试通过。
但是当我使用远程网络驱动程序在另一台机器上运行时,
我通常(并非总是),在尝试获取图片的src
时出现此错误:
Then banner image in preview html should be "p_1047_o_9075_banner_1423040308.png"
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=41.0.2272.118)
(Driver info: chromedriver=2.13.307650 (feffe1dd547ee7b5c16d38784cd0cd679dfd7850),platform=Mac OS X 10.9.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'rond-macpro.roam.corp.google.com', ip: '172.16.188.22', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.8.0_40'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/00/1lc48000h01000cxqpysvccm006dhj/T/.org.chromium.Chromium.ofRqKa}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=41.0.2272.118, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 1213beaa792477d6510f7c18cad58284
Command duration or timeout: 395 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'w.com', ip: '172.28.144.7', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-30-generic', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/00/1lc48000h01000cxqpysvccm006dhj/T/.org.chromium.Chromium.ofRqKa}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=41.0.2272.118, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, webdriver.remote.sessionid=bdc90e5b-c05a-4685-b5fa-5a7c4b65347c, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: bdc90e5b-c05a-4685-b5fa-5a7c4b65347c
这与waitDriver只是找到不同的图像名称然后得到的其他情况不同:
Then banner image in preview html should be "p_1047_o_9075_banner_1423040308.png"
org.openqa.selenium.TimeoutException: Timed out after 5 seconds waiting for w.testing.web.utils.WebExtensions$$Lambda$3/925829785@5c1b89ac
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'w.com', ip: '172.28.144.7', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-30-generic', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
这是我验证图片src的代码:
webExtensions.waitForSrc
(getSelectorForImageElement(ImageElements.BANNER), BANNER_IMAGE_BASE_PATH + expectedImageName);
public void waitForSrc(By by, String expectedImageName) {
waitForCondition(input ->
{
try {
WebElement webElement = input.findElement(by);
if (webElement == null) {
return false;
} else {
String currentSrc = webElement.getAttribute("src");
System.out.println(currentSrc);
return currentSrc.contains(expectedImageName);
}
} catch (TimeoutException exception) {
exception.printStackTrace();
//java.util.logging.
return false;
}
});
}
public void waitForCondition(Predicate<WebDriver> condition) throws TimeoutException {
WebDriverWait driverWait = new WebDriverWait(driver, 5);
driverWait.until(condition);
}
答案 0 :(得分:0)
可以通过多种方式处理此问题。 在这个主题中有一些很好的建议: Selenium WebDriver How to Resolve Stale Element Reference Exception?