Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default browser of casperjs to chrome (Change the user-agent string)

I found out that the default browser of casperjs is safari, because when I tried to access this site https://z1.expertchoice.com using casper and created a screenshot.

How can I change the default browser to chrome?

like image 842
chitcharonko Avatar asked Sep 18 '14 02:09

chitcharonko


People also ask

How do I change the default user agent on Chrome?

Option 1 – Install User-Agent Switcher Select the “Add to Chrome” button for User-Agent Switcher at the Chrome Web Store. Right-click a blank area of any web page, then choose “User-Agent Switcher“. Select the desired user agent.

How do I change the default browser in Google Chrome?

Open Google Chrome. Press the Ctrl + Shift + I keys to open its Developer Tools. In Developer Tools, click on the menu button with three vertical dots. In the menu, choose More tools - Network conditions. Go to the Network conditions tab and disable the option Select automatically. Click on the Custom list and choose the desired browser to emulate.

How to change the user agent in Safari?

This option is available in Safari’s normally hidden Develop menu. To enable it, click Safari > Preferences. Select the “Advanced” tab and enable the “Show Develop menu in menu bar” option at the bottom of the window. Click Develop > User Agent and select the user agent you want to use in the list.

How to change user-agent in Firefox?

You can again select from a predefined list or enter custom user-agent string by choosing “Other…” In Firefox, it is possible to change user-agents via the browser’s built-in settings. However, it is not as user-friendly as on Chrome or Safari. It is a real pain to use the browser’s built-in feature.


2 Answers

CasperJS doesn't use Safari. In fact, it can only use PhantomJS and SlimerJS headless browsers for its automation. Therefore, it cannot work with Chrome either.

You are probably hitting a site that does user-agent detection. When a browser makes an HTTP request, it typically includes a request header called User-Agent which contains information used to identify the browser and other technologies and their versions. For instance:

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36

As you have discovered, you can configure your user-agent string in CasperJS using:

casper.userAgent('Your User-Agent String Here');

There are many lists online of well known user-agent strings.

like image 113
Brad Avatar answered Oct 22 '22 00:10

Brad


create parameters

You can also set it on create:

var casper = require('casper').create({
    pageSettings: {
        userAgent: 'mystery browser'
    }
});

If you pass an empty string, you get: User-Agent: Mozilla/5.0. Which is likely a sensible prefix to your userAgent so that PhantomJS will get better output form websites.

You may also be interested in websites which contain common Firefox user agent strings like this one: http://www.useragentstring.com/pages/Firefox/ to make it look like you are a real user.