javascript 将casperjs的默认浏览器改为chrome(更改user-agent字符串)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25903370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:12:23  来源:igfitidea点击:

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

javascriptcasperjs

提问by chitcharonko

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

我发现casperjs的默认浏览器是safari,因为当我尝试使用casper访问这个网站https://z1.expertchoice.com并创建了一个截图。

How can I change the default browser to chrome?

如何将默认浏览器更改为 chrome?

回答by Brad

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.

CasperJS 不使用 Safari。实际上,它只能使用 PhantomJS 和 SlimerJS 无头浏览器进行自动化。因此,它也不能与 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-Agentwhich contains information used to identify the browser and other technologies and their versions. For instance:

您可能正在访问一个进行用户代理检测的站点。当浏览器发出 HTTP 请求时,它通常包含一个被调用的请求标头User-Agent,其中包含用于识别浏览器和其他技术及其版本的信息。例如:

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.

网上有很多著名的用户代理字符串列表