var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: false,
loadPlugins: false,
javascriptEnabled: true,
userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
}
});
var x = require("casper").selectXPath;
casper.start("https://kit.kijiji.ca/Index.aspx");
// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
// maybe make it a little fancier with the code from the PhantomJS equivalent
});
// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
// CasperJS doesn't provide `onResourceTimeout`, so it must be set through
// the PhantomJS means. This is only possible when the page is initialized
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
casper.then(function() {
this.sendKeys('input[name="login_username"]', 'example@example.com');
this.sendKeys('input[name="login_password"]', 'password');
});
casper.thenClick('button', function() {
this.echo("Button clicked.");
});
casper.wait(500, function() {
casper.click(x("//input[@value='Login / Ouvrir une session']"));
});
casper.wait(1500, function() {
this.echo(this.getTitle());
this.download(this.getCurrentUrl(), 'logged_in_info.html');
this.capture('example.png');
casper.exit();
});
casper.run();
我目前正在运行PhantomJS 2.1.1和最新版本的CasperJS。我整天都试图让这个工作起来但却没有希望。
我正在打开网页,填写凭据,然后登录。这一切都很好但我遇到的问题是,当我下载源代码时它完全不同于源代码我会得到的只是从我的浏览器登录。
我相信CasperJS中没有执行某种Javascript文件导致数据丢失。
例如,当我使用浏览器登录时,我能够看到当我使用CasperJS登录时不存在的数据表。
任何帮助都会非常壮观。谢谢。