admin管理员组文章数量:1327069
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
1 Answer
Reset to default 6I 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
版权声明:本文标题:javascript - How to get full interpreted html source with iframes in PhantomJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211440a2433785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论