Why can't CasperJS show part of a web site 

2019-06-04 20:30发布

问题:

I have problem of capturing everything of a web page. After I log in to outlook.com using regular browser, it should show the inbox message in the right side:

However, when I use CasperJS, it is just blank. Does anyone have any idea?

I have include a temporary login id in the script, you can test it if you can, thanks.

Here is the script:

var casper = require('casper').create({
  verbose: true,
  logLevel: "info",
  viewportSize: {width: 1280,height: 720},
  pageSettings: {userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"}
});

casper.start('http://www.hotmail.com').then(function () {
  console.log('got here');
});

casper.wait(500, function () {
  console.log('wait');
});

casper.then(function(){
  this.sendKeys("[name='loginfmt']", 'peterwhite12345678@outlook.com');
  this.sendKeys("[name='passwd']", '12345678peterwhite');
  this.click("[type='submit']");
  console.log('entering log in information');
  });

casper.wait(5000, function () {
  console.log('wait');
});

casper.then(function (){
    console.log('printscreen');
    casper.capture('there_is_nothing_by_the_right_side.png')
});

casper.run();

I have also tried something like

casperjs --ssl-protocol=any outlook.js

Should I maybe add any path/plugin to support this CasperJS?

回答1:

Try the below code. You need to give/provide enough time to open web page

var casper = require('casper').create({
// verbose: true,
// logLevel: "info",
viewportSize: {width: 1280, height: 720},
pageSettings: {userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"}
});

casper.start('http://www.hotmail.com').then(function () {
    console.log('got here');
});

casper.wait(1000, function () {
    console.log('wait');
});

casper.then(function () {
    this.sendKeys("[name='loginfmt']", 'peterwhite12345678@outlook.com');
    this.sendKeys("[name='passwd']", '12345678peterwhite');
    this.click("[type='submit']");
    console.log('entering log in information');
});

casper.wait(20000, function () {
    this.waitForSelector('#O365_MainLink_Settings', function () {
        this.test.assertExists('#O365_Lync_ButtonID', 'Lync icon is visble, hence confirmed that page opened completely');
    });
});

casper.waitForSelector(('._rp_52 > div:nth-child(4)'), function () {
    if (this.visible("button._rp_o1:nth-child(2)")) {
        console.log("Here we go, Right side is visible");
        casper.capture('there is something.png');
    }
    else {
        console.log("Nope")
    }

});

casper.run();