简体   繁体   中英

How to change selenium user agent in selenium-webdriver nodejs land?

I'm in javascript + mocha + node land.

I have tried setting userAgent and 'user-agent' as keys on capabilities:

var webdriver = require('selenium-webdriver');
var ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X)';

var driver = new webdriver.Builder().
     ...
     withCapabilities({ 'browserName': 'firefox',
        userAgent: ua,
        'user-agent': ua,
    }).
    build();

There is this answer which says to use a firefox profile, but that's not exposed. There is no driver.FirefoxProfile nor one exposed globally nor webdriver.FirefoxProfile nor driver.profiles etc.

I have tried Googling and looking the source and the documentation but there is nothing on this.

I succesfully changed phantom's user agent using WD with this code:

var capabilities = {
    'browserName': 'phantomjs',
    'phantomjs.page.settings.userAgent':  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.11 Safari/537.36'
};
return browser
    .init(capabilities)
...

And this link shows how to change firefox's user agent, although the code provided is for C#/Ruby.

You just need to install the firefox-profile package. Here's a snippet:

var webdriver = require('selenium-webdriver');
var FirefoxProfile = require('firefox-profile');

var myProfile = new FirefoxProfile();        
var capabilities = webdriver.Capabilities.firefox();

// here you set the user-agent preference 
myProfile.setPreference('general.useragent.override', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36');

// attach your newly created profile 
myProfile.encoded(function(encodedProfile) {
    capabilities.set('firefox_profile', encodedProfile);

    // start the browser 
    var wd = new webdriver.Builder().
        withCapabilities(capabilities).
        build();

    wd.get('http://testingsite.com/');
});

Easy peasy!

You cannot do it with Firefox, but you can do it with Chrome. It's undocumented:

var chrome = require('selenium-webdriver/chrome');

var opts = new chrome.Options();
opts.addArguments(['user-agent="YOUR_USER_AGENT"']);

var driver = new webdriver.Builder().
    withCapabilities(opts.toCapabilities()).
    build();

for chrome you may make like this:

var driver = new webdriver.Builder()
.usingServer('http://localhost:4444/wd/hub')
.withCapabilities({browserName: 'chrome', chromeOptions: {args:['user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"'] } })
.build();

Recent selenium-webdriver versions have added a feature to specify firefox preferences .

import {Builder, WebDriver} from 'selenium-webdriver';
import {Options} from "selenium-webdriver/firefox";

const options = new Options().setPreference('general.useragent.override', '....');
const builder = new Builder().forBrowser('firefox')
      .setFirefoxOptions(options);

const webDriver = this.createClientBuilder().build();

The answer is that it is impossible .

https://code.google.com/p/selenium/issues/detail?id=6189

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