我需要更改驱动selenium-webdriver的PhantomJS浏览器的User-Agent。
我找到了methods to change the user agent in C#, Ruby and Java
这是我尝试过的:
var webdriver = require('selenium-webdriver');
var useragent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0";
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.phantomjs("phantomjs.page.settings.userAgent", useragent)).
build();
Web服务器上的结果仍然如下所示:
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34
答案 0 :(得分:2)
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.phantomjs()
.set("phantomjs.page.settings.userAgent", useragent))
.build();
这是一个对我有用的解决方案。