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
How to change selenium user agent in selenium-webdriver nodejs land?
提问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.FirefoxProfile
nor one exposed globally nor webdriver.FirefoxProfile
nor driver.profiles
etc.
有这个答案说要使用 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();