“过时的元素参考"错误行为不理解 [英] "Stale element reference" error behavior undestanding

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

问题描述

代码 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.

protractor/WebdriverJS 不应该原生地处理这个问题.

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

1>您能否解释一下为什么代码 1 和代码 2 有时有效而有时失败?

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

2>您认为 Code 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.

推荐答案

我认为我们不能肯定为什么会发生这种情况,但你绝对不是一个人 (1) (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天全站免登陆