eyt*_*tac 5 javascript selenium webdriver selenium-webdriver
我正在运行 node.js 和 Selenium WebDriverJS。我的一项测试失败并出现以下错误:
UnknownError: unknown error: Runtime.evaluate threw exception: Error: element is not attached to the page document
Run Code Online (Sandbox Code Playgroud)
我知道这本质上是一个 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);
}
Run Code Online (Sandbox Code Playgroud)driver.sleep(1000)在查找和单击元素之前等待一个明确的时间段 ( ).findElement()在单击元素之前多次查找元素(使用)使用承诺链来捕获任何错误并尝试重新单击该元素
driver.getTitle().then(function(title) {
driver.findElement(webdriver.By.xpath(...)).click();
}).thenCatch(function(e) {
driver.findElement(webdriver.By.xpath(...)).click();
});
Run Code Online (Sandbox Code Playgroud)使用带有递归函数的承诺链来不断尝试重新单击元素
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);
});
Run Code Online (Sandbox Code Playgroud).then()代替.thenCatch()Selenium 似乎无法捕获此特定错误。我使用打印语句来确认其他错误,例如 NoSuchElementError 被.thenCatch(). 是否有一种解决方法可以让我处理陈旧的元素?
我也遇到了类似的问题,所以我做了以下解决方法,你可以尝试一下......
/*
* 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;
}
Run Code Online (Sandbox Code Playgroud)
用法示例(针对您的情况):
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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |