Phantomjs can't load iframe

2019-08-27 06:15发布

I'm a newbie, recently started to use phantomjs with casperjs. I want to get info from an iframe but phantomjs fails to load it.

This is my script:

var casper = require('casper').create({
   verbose: true,
   logLevel: "debug",
   waitTimeout: 20000,
   retryTimeout: 100,
   viewportSize: {
     width: 1920,
     height: 1080
   },
   pageSettings: {
       "userAgent": 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1'
   },
   localToRemoteUrlAccessEnabled: true
});

casper.start();

casper.open('http://www.badboysbarber.ru/online');

casper.waitForSelector('.y-main-container', function() {
   this.echo("Selector appeared.");
});

casper.then(function() {
   this.capture('screen.png');
});

casper.run();

So, phantom throws an error (although selector is defined correctly):

[error] [phantom] Wait timeout of 20000ms expired, exiting.

Could somebody help me please? Maybe I'm doing something wrong? Thank you.

1条回答
家丑人穷心不美
2楼-- · 2019-08-27 07:07

An iframe loads a document inside another document. If you want to work with frames and use CasperJS to get data, you will probably need withFrame() in Casper.prototype.

The following script captures the raw HTML content of the first iframe in your main page:

var casper = require('casper').create({
  viewportSize: {
    width: 1920,
    height: 1080
  },
  pageSettings: {
    'userAgent': 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1'
  },
  localToRemoteUrlAccessEnabled: true
});

casper.start('http://www.badboysbarber.ru/online');

casper.withFrame(0, function () {
  this.echo(this.getHTML()); // HTML code of the first iframe
});

casper.run();
查看更多
登录 后发表回答