- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在运行 node.js 和 Selenium WebDriverJS。我的一项测试因以下错误而失败:
UnknownError: unknown error: Runtime.evaluate threw exception: Error: element is not attached to the page document
我知道这本质上是一个 StaleElementReferenceException,但我一直无法找到可靠的解决方法。我尝试了以下但没有成功:
在找到并点击元素之前等待元素出现在页面上
waitForElement: function (selector, timeout) {
if (typeof(timeout) === 'undefined') { timeout = 3000; }
driver.wait(function() {
return driver.findElements(selector).then(function(list) {
return list.length > 0;
});
}, timeout);
}
driver.sleep(1000)
).findElement()
)使用 promise 链捕获任何错误并尝试重新点击该元素
driver.getTitle().then(function(title) {
driver.findElement(webdriver.By.xpath(...)).click();
}).thenCatch(function(e) {
driver.findElement(webdriver.By.xpath(...)).click();
});
使用具有递归函数的 promise 链来不断尝试重新单击该元素
var getStaleElement = function(selector, callback) {
var element = driver.findElement(selector);
callback(element);
}).thenCatch(function(e) {
getStaleElement(selector, callback);
});
var clickSelf = function(ele) { return ele.click() };
driver.getTitle().then(function(title) {
driver.findElement(webdriver.By.xpath(...)).click();
}).thenCatch(function(e) {
getStaleElement(webdriver.By.xpath(...), clickSelf);
});
.then()
的 errback 参数代替 .thenCatch()
似乎 Selenium 无法捕获此特定错误。我使用 print 语句来确认 .thenCatch()
捕获了其他错误,例如 NoSuchElementError。是否有一种解决方法可以让我处理陈旧的元素?
最佳答案
我遇到了类似的问题,所以我做了下面的解决方法,你可以试一试......
/*
* params.config - {
* opposite - {Boolean} - if true, will wait till negative result is reached/ error is thrown.
* maxWaitTime - {Number} - if this time exceeds, just throw an error and leave.
* waitTime - {Number} - wait time between two checks.
* expectValue - {Boolean} - where you just want it to run without error, or it should expect a value
* expectedValue - {Object} - object value it should or should not match.
* }
* params.fn - a function that returns a promise that we want to keep checking till desire value is reached
*/
function waiter(fn, config){
config = config || {};
var deffered = Driver.promise.defer(),
wt = config.waitTime || 100,
mwt = config.maxWaitTime || 3000,
timeoutReached = false,
pCall = function(){
fn().then(pThen, pCatch);
},
pThen = function(data){
if(timeoutReached) return;
if(config.expectValue){
if(config.opposite){
if(data == config.expectedValue){
setTimeout(pCall, wt);
}else{
clearTimeout(vTimeout);
deffered.fulfill(true);
}
}else{
if(data == config.expectedValue){
clearTimeout(vTimeout);
deffered.fulfill(true);
}else{
setTimeout(pCall, wt);
}
}
}else{
deffered.fulfill(true);
}
},
pCatch = function(err){
if(timeoutReached) return;
if(config.opposite){
deffered.fulfill(true);
}else{
setTimeout(pCall, wt);
}
};
pCall();
var vTimeout = setTimeout(function(){
timeoutReached = true;
if(config.opposite){
deffered.fulfill(true);
}else{
deffered.reject(new Error('timed-out'));
}
}, mwt);
return deffered.promise;
}
示例用法(针对您的情况):
var myPromise = function(){
return driver.findElement(webdriver.By.xpath(...)).click();
};
//default use
waiter(myPromise).then(function(){
console.log('finally...');
}).catch(fucntion(err){
console.log('not working: ', err);
});
// with custom timeout after 10 seconds
waiter(myPromise, {maxWaitTime: 10000}).then(function(){
console.log('finally...');
}).catch(fucntion(err){
console.log('not working: ', err);
});
关于javascript - Selenium WebDriverJS thenCatch 没有捕获 StaleElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31343731/
我有一个表单,正在尝试自动填写我的一个 Selenium 脚本。有一个表单,大约有 4 个文本字段,其 HTML 如下所示 填写完这些表格后,会出现一个输入提交按钮,其中包含以下 HTML 代码
我在测试中使用 Selenium.WebDriver 和 Selenium.Support 3.0.1,但在更新到更高版本后我的代码抛出错误。 失败代码: // Element locating
我最近开始使用 Selenide,我喜欢它所允许的流畅代码。然而,我对 ElementsCollection 确实有一个奇怪的问题。 $$("some ref").filterBy(not(attri
我正在尝试为亚马逊搜索结果创建一个基本的网络抓取工具。当我遍历结果时,有时会到达结果的第 5 页(有时仅第 2 页),然后抛出 StaleElementException。当我在抛出异常后查看浏览器时
我正在尝试为亚马逊结果创建一个基本的网络抓取工具。当我迭代结果时,有时会到达结果的第 5 页(有时仅第 2 页),然后抛出 StaleElementException。当我在抛出异常后查看浏览器时,我
我正在运行 node.js 和 Selenium WebDriverJS。我的一项测试因以下错误而失败: UnknownError: unknown error: Runtime.evaluate t
我有一个页面,其中有一个小表单,其中有一个输入字段和一个提交按钮。 提交按钮是 AJAX! 当我单击它时,它会将输入值提交到服务器,服务器要么验证它是否可接受并加载新页面,要么发现问题并添加反馈错误标
我是一名优秀的程序员,十分优秀!