“陈旧元素参考"指的是“旧元素参考".错误行为不明 [英] "Stale element reference" error behavior undestanding

查看:106
本文介绍了“陈旧元素参考"指的是“旧元素参考".错误行为不明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码1:

element(by.id('myButtonId')).click();
return element(by.id('myValidationSummaryId')).getText().then(function (val) {
    return val;
});

上面的代码可以正常工作很多次,然后开始出现以下错误

Above code worked fine many times and then it started giving below error

失败:过时的元素引用:该元素未附加到页面 文档"

"Failed: stale element reference: element is not attached to the page document"

此id'myValidationSummaryId'以前没有使用过,单击按钮可在'myValidationSummaryId'中发布来自服务端的表格和成功/失败消息.

this id 'myValidationSummaryId' is no where used before, clicking button posts form and success/failure message available from service side in 'myValidationSummaryId'.

代码2:

return element(by.id('myButtonId')).click().then(function () {
    return element(by.id('myValidationSummaryId')).getText().then(function (val) {
      return val;
    });
});

修复上面的代码已解决了原始问题,并且多次都能正常工作,但后来又开始随机失败,并出现原始过时的元素引用错误.

Fixing code as above fixed original issue and it worked consistently fine many times but later it started failing randomly with original stale element reference error.

代码3:

return element(by.id('myButtonId')).click().then(function () {
    return element(by.id('myValidationSummaryId')).waitReady().then(function (isReady) {
        if (isReady) {
            return element(by.id('myValidationSummaryId')).getText().then(function (val) {
                return val;
            });
        } else {
            return 'Failed to check success/failure message';
        }
    });
});

然后我如上所述修复了代码,现在它可以正常工作了, waitReady 函数主动等待存在并显示到指定时间的元素.

Then I fixed code as above and now it works fine consistently, waitReady function actively wait for an element present and displayed up to specified time.

量角器/WebdriverJS不应本机处理此问题.

Shouldn't protractor/WebdriverJS supposed to handle this issues fine natively.

1>您能解释一下为什么Code 1和Code 2有时起作用而有时却失败了吗?

1> could you please explain why Code 1 and Code 2 sometime worked and sometime failed?

2>您认为代码3现在可以正常使用了吗?

2> do you think Code 3 is now fine and expected to work every time?

元素'myValidationSummaryId'仅在单击后使用一次,因此如果页面未完全加载且元素尚不可用,则应显示找不到元素",但为什么引用了陈旧的元素? 我已经将pageLoadTimeout用作5分钟,并且页面在几秒钟内加载完毕.这是非AngularJS应用程序和browser.ignoreSynchronization = true.

Element 'myValidationSummaryId' used only once and after click so if pages is not loaded fully and if element is yet not available it should say No Element Found but why stale element reference? I have used pageLoadTimeout as 5 minutes and page is loaded in few second. This is non AngularJS application and browser.ignoreSynchronization = true.

它到处都在讨论什么代码可以解决该问题,但是却找不到为什么这种行为以及WebdriverJS本身无法处理它的很多信息.

everywhere it talked about what code can fix it but not found much on why this behavior and why WebdriverJS itself not able to handle it.

推荐答案

我不认为我们可以肯定为什么会发生这种情况,但是您绝对不是一个人(2).这可能是由于页面的特定呈现方式(例如,您的应用程序/框架如何处理呈现DOM元素),或者仅是Selenium/驱动程序造成的.如果您对确切的解释感兴趣,那么使用Protractor错误报告系统可能会更好.

I don't think we can be certainly sure why that happens, but you're definitely not alone (1) (2). This may be due to the way your page is rendered specifically (e.g. how your app/framework is handling rendering DOM elements), or just a Selenium/driver thing. If you're interested into an exact explanation, you might have better luck if using Protractor bug report system.

但是,很好的猜测可能是它与Selenium 定义陈旧元素的方式有关:

A good guess, however, may be that it is related to the way Selenium defines stale element:

一个不太常见但仍然常见的原因是JS库所在的位置 删除了一个元素,并用具有相同ID的元素替换了该元素,或者 属性

A less common, but still common cause is where a JS library has deleted an element and replaced it with one with the same ID or attributes

某些库可能使Selenium误认为元素已从DOM中消失了,但是实际上它只是在瞬间被替换了.在单击和将元素放置在DOM中之间(基本上是竞赛条件)之间添加了紧密而脆弱的时间-可能是原因.您可能有兴趣在此处阅读更多有关它的信息.

Some libraries can fool Selenium into believing an element is gone from the DOM, but it has been really just replaced in an instant. Adding there a tight, frail timing between clicking and element being placed in the DOM (race condition basically) - that might be the cause. You might be interested in reading a little more about it here.

无论如何,如果您遇到此类问题,建议您使用browser.wait和预期条件.

Anyway, if you're having such issues, I'd recommend you using browser.wait and Expected Conditions.

期望的条件基本上是返回true或false的函数,并且您可以指定一个超时,如果在该时间内未返回true,则超时将导致测试失败-您可以查看在

Expected conditions are basically functions that return true or false, and you can specify a timeout that will cause test to fail if true is not returned in that time - you can see how it's used in a similar question.

基本上,您可能想要这样做:

Basically, you might want to do it like this:

var EC = protractor.ExpectedConditions;
var summaryId = element(by.id('myValidationSummaryId'));

browser.wait(EC.presenceOf(summaryId), 5000);
//rest of your code

这篇关于“陈旧元素参考"指的是“旧元素参考".错误行为不明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆