admin管理员组

文章数量:1326619

With PhantomJS, I want to print the html source of a webpage like Firebug does. Interpreted with iframes.

var page = require('webpage').create();

page.open('', function () {
  console.log(page.content);
  phantom.exit(); 
});

This only seem to shows the interpreted HTML without iframes html. And use evaluate can't help because my iframes are in another domain so I think javascript with not have access to them.

With PhantomJS, I want to print the html source of a webpage like Firebug does. Interpreted with iframes.

var page = require('webpage').create();

page.open('http://google.', function () {
  console.log(page.content);
  phantom.exit(); 
});

This only seem to shows the interpreted HTML without iframes html. And use evaluate can't help because my iframes are in another domain so I think javascript with not have access to them.

Share Improve this question edited Feb 14, 2015 at 9:04 kaes asked Feb 12, 2015 at 18:04 kaeskaes 911 silver badge5 bronze badges 4
  • Yes, it is possible, but you will have to write it yourself. Earlier code request: Dump HTML of page including iframes – Artjom B. Commented Feb 12, 2015 at 18:07
  • Thank you but I already made this kind of solution. It downloads again the iframes (like others page.open) but it is "a waste" since PhantomJS is a full web browser so must have already downloaded this content. But this information may be hiden to us in the interpreter. – kaes Commented Feb 12, 2015 at 21:00
  • There should be no need to download the iframes as you can simply switch to every iframe and get its content. – Artjom B. Commented Feb 12, 2015 at 21:12
  • Yes, I have found why I can't make it work, see edit of my question. – kaes Commented Feb 13, 2015 at 4:47
Add a ment  | 

1 Answer 1

Reset to default 6

I found that going through frames to get content did not work because page.framesCount in phantomjs counts only the child frames and not the main frame. Here is working code to display the HTML of all frames:

// Apparently framesCount doesn't include the main frame so add 1
var frameCount = page.framesCount + 1
var html = page.frameContent + '\n\n'
for (var i = 1; i < frameCount; ++i) {
  page.switchToFrame(i)
  html += page.frameContent + '\n\n'
}

One last important thing, if you don't want the source but want to access the iframe DOM even if it's in another domain do it like this:

phantomjs --web-security=no

The code to access the iframe body is:

var i = document.getElementsByTagName('iframe')
var body = i[0].contentWindow.document.body

本文标签: javascriptHow to get full interpreted html source with iframes in PhantomJSStack Overflow