如何更改我的 waitDriver 以克服“StaleElementReferenceException:过时的元素引用:元素未附加到页面文档”
how can I change my waitDriver to overcome `StaleElementReferenceException: stale element reference: element is not attached to the page document`
我 运行 我的自动化(黄瓜、junit、硒 chrome 网络驱动程序)。
当我在本地 运行 时,测试通过。
但是当我在另一台机器上使用远程网络驱动程序 运行 时,
我通常(不总是)在尝试获取图像的 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/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
这是我验证图像源代码的代码:
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);
}
可以通过多种方式处理此问题。
在本主题中有一些好的建议:
Selenium WebDriver How to Resolve Stale Element Reference Exception?
我 运行 我的自动化(黄瓜、junit、硒 chrome 网络驱动程序)。
当我在本地 运行 时,测试通过。
但是当我在另一台机器上使用远程网络驱动程序 运行 时,
我通常(不总是)在尝试获取图像的 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/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
这是我验证图像源代码的代码:
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);
}
可以通过多种方式处理此问题。 在本主题中有一些好的建议: Selenium WebDriver How to Resolve Stale Element Reference Exception?