问题
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