gpt4 book ai didi

javascript - Selenium WebDriverJS thenCatch 没有捕获 StaleElementException

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:49 24 4
gpt4 key购买 nike

我正在运行 node.js 和 Selenium WebDriverJS。我的一项测试因以下错误而失败:

UnknownError: unknown error: Runtime.evaluate threw exception: Error: element is not attached to the page document

我知道这本质上是一个 StaleElementReferenceException,但我一直无法找到可靠的解决方法。我尝试了以下但没有成功:

  1. 在找到并点击元素之前等待元素出现在页面上

    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);
    }
  2. 在找到并点击元素之前等待一段明确的时间(driver.sleep(1000))
  3. 在点击元素之前多次查找元素(使用 .findElement())
  4. 使用 promise 链捕获任何错误并尝试重新点击该元素

    driver.getTitle().then(function(title) {
    driver.findElement(webdriver.By.xpath(...)).click();
    }).thenCatch(function(e) {
    driver.findElement(webdriver.By.xpath(...)).click();
    });
  5. 使用具有递归函数的 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);
    });
  6. 方法 4 和 5 使用 .then() 的 errback 参数代替 .thenCatch()
  7. 上述的组合

似乎 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com