【问题标题】:No element found using locator: By(css selector, *[id="more"])使用定位器找不到元素:By(css selector, *[id="more"])
【发布时间】:2018-06-16 00:36:35
【问题描述】:

protractor.e2e-sepc.ts

import {browser, element, by} from 'protractor';

describe('Open the link', () => {

    beforeEach(() => {
        browser.waitForAngularEnabled(false);
        browser.get('url');
        browser.sleep(2000);

    });

    it('Click on more button', () => {
        element(by.id('more').click();
        })
    })

当我在 chrome 上作为浏览器运行上述测试用例时,它运行成功,但是当我用 chrome --headless 浏览器运行它时,它通过将错误显示为No element found using locator: By(css selector, *[id="more"]) 来失败规范

protractor.config.js

multiCapabilities: [{
        'browserName': 'chrome',
        'chromeOptions': {
            args: ["--headless", "--disable-gpu"]
        }
}]

【问题讨论】:

    标签: protractor e2e-testing google-chrome-headless


    【解决方案1】:

    我的假设是 2 秒的睡眠是不够的,您只需要 explicitly wait 即可获得所需元素的存在:

    var more = element(by.id('more'));
    var EC = protractor.ExpectedConditions;
    
    browser.wait(EC.presenceOf(more), 10000)   
    more.click();
    

    请注意,您缺少右括号,但我认为这只是一个错字:

    element(by.id('more')).click();
    //               HERE^
    

    【讨论】:

    • 嗨@alecxe我也试过你在stackoverflow.com/questions/37809915/…这篇文章中的回答中写的方法。我的测试在没有无头浏览器的情况下成功运行,在无头浏览器中失败。主要的是只有此 id 失败,否则其他所有 tcs 都在无头和无头的情况下成功运行。我也更改了 id。
    • 你能在无头模式下制作视频或截图吗?也许没有这样的元素,量角器是对的:)
    • 您也可以更改定位器并以这种方式解决您的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2021-01-16
    相关资源
    最近更新 更多