Search code examples
javascriptcasperjs

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?


Solution

  • 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.