Reputation: 1890
How can a custom window.navigator object be applied to PhantomJS's WebPage object during creation? When I attempt to customize the navigator, I'm still receiving the default value when I console.log('The default user agent is ' + page.settings.userAgent);
What I have so far:
console.log('hello');
var page = require('webpage').create();
page.onInitialized = function() {
page.evaluate(function() {
var newNavigator = Object.create(window.navigator);
newNavigator.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36';
window.navigator = newNavigator;
});
};
page.open('http://www.google.com', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
console.log('The default user agent is ' + page.settings.userAgent);
}});
The contents of the console when running the above js:
hello
The default user agent is Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34
(KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34
^C
Upvotes: 2
Views: 1409
Reputation: 61952
You don't need and cannot use evaluate
to set the navigator. You can just set via settings. So the following works for me on phantomjs 1.9.7:
page.onInitialized = function() {
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36';
};
Output
hello The default user agent is Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 ^C
Upvotes: 2