Protractor 使用locator:By(css选择器,*[id=“more”quot;]未找到任何元素

Protractor 使用locator:By(css选择器,*[id=“more”quot;]未找到任何元素,protractor,e2e-testing,google-chrome-headless,Protractor,E2e Testing,Google Chrome Headless,量角器e2e sepc.ts import {browser, element, by} from 'protractor'; describe('Open the link', () => { beforeEach(() => { browser.waitForAngularEnabled(false); browser.get('url'); browser.sleep(2000); }); it('

量角器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();
        })
    })
multiCapabilities: [{
        'browserName': 'chrome',
        'chromeOptions': {
            args: ["--headless", "--disable-gpu"]
        }
}]
当我在chrome上以浏览器的形式运行上述测试用例时,它成功地运行了,但当我使用chrome--headless浏览器运行时,它通过使用locator:by(css选择器,*[id=“more”])将错误显示为
找不到元素,从而使规范失败。

量角器.config.js

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();
        })
    })
multiCapabilities: [{
        'browserName': 'chrome',
        'chromeOptions': {
            args: ["--headless", "--disable-gpu"]
        }
}]

我的假设是,2秒钟的睡眠是不够的,您只需要为所需元素的存在:

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我也尝试了你在这篇文章的答案中所使用的方法。我的测试在没有headless浏览器的情况下成功运行,在headless浏览器中失败。主要的是,只有这个id失败,否则其他tcs在headless和headless中都会成功运行。我也更改了id。你能制作一个视频吗还是无头模式下的屏幕截图?也许并没有这样的元素,量角器是正确的:)你们也可以改变定位器,并以这种方式解决你们的问题吗?