我正在使用selenium-standalone和webdriverio,并且我试图设置自定义用户代理。
var selenium = require('selenium-standalone');
var webdriverjsAngular = require('webdriverjs-angular');
var FirefoxProfile = require('firefox-profile');
var myProfile = new FirefoxProfile();
myProfile.setPreference("general.useragent.override", "custom-user-agent");
var webDriverOptions = {
desiredCapabilities: {
browserName: 'firefox',
firefox_profile: myProfile
}
};
var seleniumOptions = {
drivers: {
chrome: {
version: '2.9',
arch: process.arch,
baseURL: 'https://chromedriver.storage.googleapis.com'
},
},
};
selenium.start(seleniumOptions, function(err, child) {
webdriverjsAngular.remote(webDriverOptions)
.....
}
代码正在运行,但未使用firefox-profile
的设置,Firefox仍在使用此用户代理:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
。
如何设置自定义用户代理?
答案 0 :(得分:0)
我错过了最后一部分:
var myProfile = new FirefoxProfile();
myProfile.setPreference("general.useragent.override", "custom-user-agent");
myProfile.encoded(function (encodedProfile) {
// output the generated encoded profile as stdout
// NOTE: if an error occurs with the encodedProfile then the default Firefox settings will be used in your tests
var webDriverOptions = {
desiredCapabilities: {
browserName: 'firefox',
firefox_profile: encodedProfile
}
};
});