是否可以在无头模式下使用Chrome使用Selenium/WebdriverIO进行自动浏览器测试?
据说Chrome --headless现在已成问题,但我无法使他们的示例正常工作。我希望 Selenium 可以为此提供选择吗?
我正在像这样初始化WebdriverIO:
const WebdriverIO = require('webdriverio');
let driver = WebdriverIO.remote({
desiredCapabilities: {
browserName: browser, // "chrome" or "firefox"
},
});
我正在使用selenium-standalone启动Selenium:
selenium-standalone start > /dev/null 2>&1
最佳答案
WebdriverIO
这是WebdriverIO的工作示例:https://github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/src/index.ts
基本思路:
import * as webdriverio from 'webdriverio';
// Headless is supported in Chrome >= 58. Not currently stable, so using dev
// build.
const CHROME_BIN_PATH = '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome';
const options = {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
binary: CHROME_BIN_PATH,
args: [
'headless',
// Use --disable-gpu to avoid an error from a missing Mesa
// library, as per
// https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
'disable-gpu',
],
},
},
};
webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.getTitle().then(title => {
console.log({ title });
})
.end();
WebDriverJS
这是WebDriverJs(WebDriver的官方JavaScript客户端)的工作示例:https://github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts
基本思路:
import * as webdriver from 'selenium-webdriver';
import * as chromeDriver from 'selenium-webdriver/chrome';
// Headless is supported in Chrome >= 58. Not currently stable, so using dev
// build.
const CHROME_BIN_PATH = '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome';
const options = new chromeDriver.Options();
options.setChromeBinaryPath(CHROME_BIN_PATH);
options.addArguments(
'headless',
// Use --disable-gpu to avoid an error from a missing Mesa library, as per
// https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
'disable-gpu',
);
const driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
关于node.js - Selenium/WebdriverIO Chrome headless 吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42303119/