最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

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

How can I change the default browser to chrome?

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?

Share Improve this question edited Dec 11, 2014 at 1:11 chitcharonko asked Sep 18, 2014 at 2:45 chitcharonkochitcharonko 3312 gold badges6 silver badges18 bronze badges 7
  • Casper doesn't use Chrome, it uses PhantomJS and SlimerJS. What are you trying to do? Change the user-agent string? – Brad Commented Sep 18, 2014 at 3:16
  • sorry, i don't have any idea on what is that user-agent string. what is that by the way? – chitcharonko Commented Sep 18, 2014 at 3:37
  • When the browser connects to a server, usually one of the request headers is User-Agent. For instance, mine right now is Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36 That tells the server what browser I'm running. – Brad Commented Sep 18, 2014 at 3:38
  • is this the right way to change the user agent string? casper.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5'); – chitcharonko Commented Sep 18, 2014 at 3:40
  • Yes, exactly. That's correct. – Brad Commented Sep 18, 2014 at 3:41
 |  Show 2 more comments

2 Answers 2

Reset to default 11

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.

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.

发布评论

评论列表(0)

  1. 暂无评论