Trying to follow a link in an iframe using CasperJS

纵饮孤独 提交于 2019-12-13 17:07:37

问题


I'm trying to use CasperJS to follow a link that's in an iframe but I can't seem to get at the iframe's document.

Here's a test using an iframe example page I found. The third iframe has a name attribute which I need for Casper's frame method. Casper API

var casper = require('casper').create();

casper.start('http://nunzioweb.com/iframes-example.htm');

casper.withFrame('frame', function(){
  this.echo('Page url is ' + this.getCurrentUrl());
  this.echo(this.getHTML());
});

casper.run();

The page url comes back with "http://nunzioweb.com/lyrics/455Rocket.html" as I expected but the returned html is the page wrapping the iframe.

Any idea how I can get into the iframe so I can click a link [on the page I'm actually doing this with]?


回答1:


For some reason .getHTML() doesn't work here, you have to get the page HTML directly from the WebPage instance.

var casper = require('casper').create();

casper.start('http://nunzioweb.com/iframes-example.htm');

casper.withFrame('frame', function(){
    this.echo('Page url is ' + this.getCurrentUrl());
    this.echo(this.page.content);
});

casper.run();

I'll be working on that issue.

To click on a link, eg. on the Slick City one in your iframe example:

var casper = require('casper').create();

casper.start('http://nunzioweb.com/iframes-example.htm');

casper.withFrame('frame', function() {
    this.echo('Page url is ' + this.getCurrentUrl());
    this.clickLabel('Slick City');
});

casper.waitForPopup('slickcitydown.htm').withPopup('slickcitydown.htm', function() {
    this.echo('New page url is ' + this.getCurrentUrl());
});

casper.run();

That gives:

$ casperjs test.js 
Page url is http://nunzioweb.com/lyrics/455Rocket.html
New page url is http://nunzioweb.com/slickcitydown.htm



回答2:


It looks like in last versions the issue was fixed.

I'm using the code example from the question with casperjs v.1.0.2 and I'm getting the html of the iframe http://nunzioweb.com/lyrics/455Rocket.html as it's expected.



来源:https://stackoverflow.com/questions/13942651/trying-to-follow-a-link-in-an-iframe-using-casperjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!