我有一段代码试图查看元素中是否存在文本
browser.wait(protractor.ExpectedConditions.not(protractor.ExpectedConditions.textToBePresentInElement(self.btnDashboards(),dashboardName)));
,其中
self.btnDashboards = function() {
return element(this.selectors.btnDashboards);
};
var dashboardName = 'foo';
然而,这偶尔会返回
Fatal error: 09:54:11.129 WARN - Exception thrown org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=44.0.2403.155) (Driver info: chromedriver=2.15.322455 (ae8db840dac8d0c453355d3d922c91adfb61df8f),platform=Mac OS X 10.10.4 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 182 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50' System info: host: 'Sujits-MacBook-Pro.local', ip: '10.80.132.90', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.4', java.version: '1.8.0_51' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/tc/89n0t1qs5p3fyw_n9hk1x7fc0000gn/T/.org.chromium.Chromium.QSwwCm}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=44.0.2403.155, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: bbd4b5004df6ff3855bccacf8d16e8c0 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268) at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:152) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement$1.invoke(EventFiringWebDriver.java:331) at com.sun.proxy.$Proxy2.getText(Unknown Source) at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.getText(EventFiringWebDriver.java:381) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.openqa.selenium.remote.server.KnownElements$1.invoke(KnownElements.java:63) at com.sun.proxy.$Proxy3.getText(Unknown Source) at org.openqa.selenium.remote.server.handler.GetElementText.call(GetElementText.java:29) at org.openqa.selenium.remote.server.handler.GetElementText.call(GetElementText.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:168) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
是否有更好的方法对其进行编码,以便它根本不会发生?另外为什么偶尔会发生这种情况而不是经常发生?
答案 0 :(得分:0)
因为browser.wait返回一个promise,你可以使用.then({传递函数},{失败函数})来捕获错误并再试一次。这是我用的。
browser.prototype.wait2 = function (waitFunction, timeout, timeoutMessage) {
var self = this;
return browser.wait(waitFunction, timeout, timeoutMessage).then(function(pass){return pass}, function(error){
//retry if the error is a stale element, half the timeout
if(error.name == "StaleElementReferenceError") {
return self.wait2(waitFunction, timeout/2, timeoutMessage);
} else {
throw error;
}
});
};