简体   繁体   中英

Set user-agent with selenium-webdriver and phantomjs in nodejs

I need to change the User-Agent of my PhantomJS browser that's driving selenium-webdriver.

I've found methods to change the user agent in C#, Ruby and Java

This is what I've tried:

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();

The result on the web server still looks like this: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34

    var driver = new webdriver.Builder()
       .withCapabilities(webdriver.Capabilities.phantomjs()
       .set("phantomjs.page.settings.userAgent", useragent))
       .build();

This is a solution that worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM