最近,Selenium发布了他们的主要版本Selenium 4,并宣布从Selenium4开始将不再对PhantomJS提供任何支持。这是不是意味着Selenium不再支持无头自动化,或者在Selenium版本4中有什么方法可以在无头模式下执行测试?欣赏代码示例。
发布于 2019-05-08 15:05:25
在Selenium 4中,删除了对PhantomJS的本机支持。不过,使用PhantomJS在无头模式下运行脚本的用户可以在无头模式下使用Chrome或Firefox,如下所示。
const chrome = require('../chrome');
const firefox = require('../firefox');
const {Builder, By, Key, until} = require('..');
const width = 640;
const height = 480;
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
new chrome.Options().headless().windowSize({width, height}))
.setFirefoxOptions(
new firefox.Options().headless().windowSize({width, height}))
.build();
driver.get('http://www.google.com/ncr')
.then(_ =>
driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
.then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
.then(
_ => driver.quit(),
e => driver.quit().then(() => { throw e; }));要了解有关Selenium 4更改的更多信息,请参阅Selenium 4 (alpha) is Released: What’s New?
https://stackoverflow.com/questions/56035177
复制相似问题