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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18501387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:10:28  来源:igfitidea点击:

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

javascriptnode.jsselenium

提问by Andy Ray

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 answerwhich says to use a firefox profile, but that's not exposed. There is no driver.FirefoxProfilenor one exposed globally nor webdriver.FirefoxProfilenor driver.profilesetc.

这个答案说要使用 firefox 配置文件,但没有公开。没有driver.FirefoxProfile或没有一个在全球范围内暴露,也webdriver.FirefoxProfile没有driver.profiles等等。

I have tried Googling and looking the sourceand the documentationbut there is nothing on this.

我试过谷歌搜索并查看源代码文档,但没有任何内容。

采纳答案by Andy Ray

You cannotdo it with Firefox, but you cando it with Chrome. It's undocumented:

不能用 Firefox 来做,但你可以用 Chrome 来做。这是无证的:

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

回答by pyrho

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

我使用WD和以下代码成功更改了 phantom 的用户代理:

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 thislink shows how to change firefox's user agent, although the code provided is for C#/Ruby.

这个链接显示如何更改Firefox的用户代理,但所提供的代码是C#/ Ruby的。

回答by gyss

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

你只需要安装firefox-profile包。这是一个片段:

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!

十分简单!

回答by Oleg Pazdnikov

for chrome you may make like this:

对于 chrome,你可以这样制作:

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