Casper.js获取网页和显示

时间:2017-12-06 11:47:47

标签: javascript html casperjs

有没有办法使用casper.js登录页面然后显示页面? 我可以拍摄我希望它成为实际页面的屏幕截图。

这是我到目前为止所做的,虽然我确定它非常错误:

var casper = require('casper').create({
 viewportSize: {
    width: 1365,
    height: 768
},
// engine: 'slimmerjs',
verbose: true,
logLevel: "debug",
pageSettings: {
loadImages: false,//The script is much faster when this field is set to false
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36  (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
}
});

//First step is to open
casper.start().thenOpen("http://mysite/login", function() {
console.log("mysite website opened");
});

//Now we have to populate username and password, and submit the form
casper.then(function(){
console.log("Login using username and password");
this.evaluate(function(){
document.getElementById("user_email").value="me";
document.getElementById("user_password").value="password";
document.getElementById('new_user').submit();
});
});
casper.thenOpen('http://mysite/report/70?dashboard_id=2', function(){
console.log("Make a screenshot of morning info and save it as  pic.png"); 
this.wait(10000, function(){
this.captureSelector('pic.png','.highcharts- container');
});
});

casper.start("http://mysite/report/70?dashboard_id=2", function() {
this.page.switchToChildFrame(0);
this.page.switchToParentFrame();
});

casper.run(function() {
  this.exit();
});


casper.run();

1 个答案:

答案 0 :(得分:1)

如果你想捕获HTML,这就是你在Casper中的做法。

在您想要此捕获的位置(登录后...),在快照的同一等待中..试试这个:

this.wait(10000, function(){
    this.captureSelector('pic.png','.highcharts- container');

    var HTML = document.createElement('textarea);
    HTML.innerHTML = this.getHTML();
    console.log(HTML.value);  // or use the variable for something else
});

使用CasperJS 4年来执行网页抓取,这是我能设计的唯一方法,并且给了我很好的可解析HTML。