我正在尝试使用 CasperJS 来自动处理大量繁琐的数据输入。具体来说,要在佳能网站上注册非常大批量的相机设备。
我有处理表单填写的代码,并且使用 CasperJS 浏览网页没有问题,但我无法让 CasperJS 导航到登录页面。
这是我到目前为止所拥有的:
var casper = require('casper').create({
verbose: true,
userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
logLevel: "debug"
});
phantom.cookiesEnabled = true;
casper.start();
casper.thenOpen('https://b2cweb.usa.canon.com/b2cweb/view/myAccountHome.jsf?LOGINACTION=Y', function(response) {
require('utils').dump(response);
});
casper.run();
注意:一旦到达页面,上面的代码就会转储标题内容。我还尝试了不同的用户代理字符串,以防 Adobe 主动阻止爬虫。
该链接在 Google Chrome 中完美运行,但 CasperJS 只记录以下内容。
Loading resource failed with status=fail
从 Google Dev Tools 看来,链接 302 重定向到
https://b2cweb.usa.canon.com/b2cweb/view/myAccountHome.jsf?LOGINACTION=Y
然后 301 重定向到:
我假设重定向是 CasperJS 无法获取登录页面的原因。
是否可以让 CasperJS 遵循这些类型的重定向? CasperJS 可以用于通过 HTTPS 浏览和/或登录吗?
更新:
我不太确定是否是导致问题的重定向。
作为一种解决方法,我尝试将 session cookie(从 chrome 复制)预加载到 casperjs,然后直接导航到帐户页面。
我还尝试将 --ignore-ssl-errors=true 标志添加到命令中,但没有成功。
无论我做什么,它都会返回一个 about:blank url。
最佳答案
以下将带您进入登录页面:
var casper = require("casper").create ({
waitTimeout: 15000,
stepTimeout: 15000,
verbose: true,
viewportSize: {
width: 1400,
height: 768
},
onWaitTimeout: function() {
logConsole('Wait TimeOut Occured');
this.capture('xWait_timeout.png');
this.exit();
},
onStepTimeout: function() {
logConsole('Step TimeOut Occured');
this.capture('xStepTimeout.png');
this.exit();
}
});
casper.on('remote.message', function(msg) {
logConsole('***remote message caught***: ' + msg);
});
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4');
// vars
var gUrl = 'http://www.usa.canon.com/cusa/home';
// Open URL and click sign in
casper.start(gUrl, function() {
this.clickLabel('Sign In', 'a');
});
//Sign in page
casper.then(function () {
//+++ ready for you to fill user information.
this.capture('xSignIn.png'); //+++ shows you are on signin page. can remove.
});
casper.run();
关于redirect - CasperJS 没有遵循重定向到 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19939062/