简体   繁体   English

如何在 selenium-webdriver nodejs 土地中更改 selenium 用户代理?

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

I'm in javascript + mocha + node land.我在 javascript + mocha + node 领域。

I have tried setting userAgent and 'user-agent' as keys on capabilities:我曾尝试将 userAgent 和 'user-agent' 设置为功能键:

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.这个答案说要使用 firefox 配置文件,但没有公开。 There is no driver.FirefoxProfile nor one exposed globally nor webdriver.FirefoxProfile nor driver.profiles etc.没有driver.FirefoxProfile也没有一个全局公开的,也没有webdriver.FirefoxProfiledriver.profiles等。

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: 我使用此代码成功地使用WD更改了幻影的用户代理:

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. 链接显示了如何更改firefox的用户代理,尽管提供的代码是针对C#/ Ruby的。

You just need to install the firefox-profile package. 您只需要安装firefox-profile包。 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. 不能用Firefox做到这一点,但你可以用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 .最近的 selenium-webdriver 版本添加了一个功能来指定 firefox 首选项

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 https://code.google.com/p/selenium/issues/detail?id=6189

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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