最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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'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.

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Aug 29, 2013 at 2:30 Andy RayAndy Ray 32.1k16 gold badges113 silver badges148 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 5

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./');
});

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

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

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

The answer is that it is impossible.

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

发布评论

评论列表(0)

  1. 暂无评论