【问题标题】:How to resolve staleElementReferenceError: stale element reference: element is not attached to the page document in protractor?如何解决 staleElementReferenceError: stale element reference: element is not attach to the page document in protractor?
【发布时间】:2019-10-10 14:13:24
【问题描述】:

我的应用有超过 1 个登录按钮,在测试各种功能时,它在 12 次迭代中的第 1 次和第 8 次迭代失败。需要建议来修复代码。

public async clickLoginMenu() {
    try {
        const button = await element(await by.buttonText('Login'));

           /* await browser.executeScript('return arguments[0].click()', await button).then(() => {
                browser.sleep(2000);
            });*/

            await browser.wait(until.presenceOf(await button), TIMEOUT_MILLIS,
                'Unable to locate logout button.');
            await button.click();
        } else {
            logger.info('Cannot find the login button to click');
        }
    } catch (e) {
        logger.error('Throw Exception error ' + e);
    }

}

【问题讨论】:

    标签: typescript jasmine protractor


    【解决方案1】:

    您可以尝试在presenceOf 和实际的click() 之间添加一个elementToBeClickable 等待

    所以:

    1. 等待出现
    2. 等待点击
    3. 点击它

    【讨论】:

    • 我试过了,但它进入了无限循环,说明该定位器找到了多个元素。然后我将其更改为 CSS 而不是 ButtonText。它抛出 NoSuchElementError: No element found using locator: By(css selector, #navbarmobile > div > button:nth-child(4))
    • 我再次尝试了一些更改。它确实奏效了!非常感谢你的建议。此外,由于它具有多个具有相同 buttonText 名称的元素。是否可以按索引或某个值进行选择? elements.all 然后通过元素过滤找到第一个等..
    • 是的,当然,你总是可以像element.all(by.css('.button')).get(index);一样去
    【解决方案2】:

    下面是我的工作代码:

    public async clickLoginMenu() {
        try {
            const button = await element(await by.buttonText('Login'));
            if (await button.isDisplayed()) {
            await browser.wait(until.elementToBeClickable(await button), TIMEOUT_MILLIS,
                    'Unable to locate logout button.');
            await button.click();
            } else {
                logger.info('Cannot find the login button to click');
            }
        } catch (e) {
            logger.error('Throw Exception error ' + e);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2023-02-21
      • 2020-05-13
      相关资源
      最近更新 更多