[英]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. 我发现casperjs的默认浏览器是safari,因为当我尝试使用casper访问此站点https://z1.expertchoice.com并创建了一个屏幕截图。
How can I change the default browser to chrome? 如何将默认浏览器更改为chrome?
CasperJS doesn't use Safari. CasperJS不使用Safari。 In fact, it can only use PhantomJS and SlimerJS headless browsers for its automation.
事实上,它只能使用PhantomJS和SlimerJS无头浏览器进行自动化。 Therefore, it cannot work with Chrome either.
因此,它也无法与Chrome一起使用。
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. 当浏览器发出HTTP请求时,它通常包含一个名为
User-Agent
的请求标头,其中包含用于标识浏览器及其他技术及其版本的信息。 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: 如您所知,您可以使用以下命令在CasperJS中配置用户代理字符串:
casper.userAgent('Your User-Agent String Here');
There are many lists online of well known user-agent strings . 有许多众所周知的用户代理字符串在线列表 。
create
parameters create
参数
You can also set it on create
: 您也可以在
create
设置它:
var casper = require('casper').create({
pageSettings: {
userAgent: 'mystery browser'
}
});
If you pass an empty string, you get: User-Agent: Mozilla/5.0
. 如果传递一个空字符串,则会得到:
User-Agent: Mozilla/5.0
。 Which is likely a sensible prefix to your userAgent
so that PhantomJS will get better output form websites. 这可能是您的
userAgent
的合理前缀,因此PhantomJS将从网站获得更好的输出。
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. 您可能还对包含常见Firefox用户代理字符串的网站感兴趣,例如: http : //www.useragentstring.com/pages/Firefox/ ,使其看起来像您是真正的用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.