Ben Muircroft
Ben Muircroft

Reputation: 3034

How to set the user agent string in the phantom module?

var phantom = require('phantom');
console.dir(phantom);
phantom.create(function(browser){
    browser.createPage(function(page){
        page.customHeaders={
            "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36",
            };
        console.dir(page.settings);
        //undefined
        page.settings={};
        page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
        page.settings.HTTP_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
        console.dir(page.settings);
        page.open('http://example.com/req.php', function() {
            setTimeout(function() {
                var output = page.evaluate(function() {
                    return document;
                    });
                console.dir(output);
                //undefined
                }, 1000);
             });});});

when I use phantomjs I try and set the header for userAgent using three different ways but when I visit the page and save the PHP $_SERVER object to a txt pad I still see PhantomJS

HTTP_USER_AGENT: Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.1-development Safari/538.1

not only that but the output of the page is also undefined.

It seems that the docs have changed or I cant find the correct ones. I am looking at

http://phantomjs.org/api/webpage/property/settings.html

https://www.npmjs.com/package/phantom

How is this used correctly?

Upvotes: 9

Views: 13083

Answers (2)

SlovyanskiyYehor
SlovyanskiyYehor

Reputation: 466

Currently [ 27.01.2018 ], with these requirements: phantom: ^4.0.12, webpage: ^0.3.0

i use this method to set up this property:

page.setting(key, value);

I checked it out with php in $_SERVER array. It works correctly.

Сompletely code looks like this:

const phantom = require('phantom');

(async function() {
  const instance = await phantom.create();
  const page = await instance.createPage();
  page.setting('userAgent',"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11");

  await page.on('onResourceRequested', function(requestData) {
    //Dump request settings to view result of our changes:
    console.info('Requesting', requestData);
  });

  const status = await page.open('https://stackoverflow.com');
  const content = await page.property('content');
  //console.log(content);

  await instance.exit();
})();

Upvotes: 3

Artjom B.
Artjom B.

Reputation: 61952

According to the Functional Details in the docs, you have to set the user agent through page.set():

page.set('settings.userAgent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36');

It has to be done this way, because the bridge has to communicate with the PhantomJS process and isn't doing this in a non-asynchronous fashion. This could've probably been implemented with Object.defineProperty.

If you want to set multiple settings at once, you can do (ref):

page.set('settings', {
    userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
    javascriptEnabled: false,
    loadImages: false
});

You can find a list of settings that you can set in page.settings.

Upvotes: 12

Related Questions