【发布时间】:2014-04-06 06:45:30
【问题描述】:
我在访问 iframe 的 contentDocument 时遇到了困难。我正在使用 phantomjs (1.9)。我已经查看了各种线程,但似乎没有一个答案。
这是我注入 jquery 以尝试选择元素的 phantomjs 脚本。
var page = require('webpage').create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg);
};
page.onError = function(msg) {
console.log('ERROR MESSAGE: ' + msg);
};
page.open('http://localhost:8080/', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
console.log( $('iframe').contentDocument.documentElement );
});
phantom.exit();
});
});
除了 jquery 之外,我还使用这两行代码来获取我想要的 DOM 元素(iframe 内的 DOM HTML 元素)。 PhantomJS 似乎无法解析 getElementsByTagName('iframe') 或 $('iframe') 之外的任何内容,可能是因为它还没有完成加载吗?
document.getElementsByTagName('iframe')[0].contentDocument.activeElement;
document.getElementsByTagName('iframe')[0].contentDocument.documentElement;
我也在运行脚本,禁用 --web-security=no 设置
【问题讨论】:
-
还没有答案...嗯
标签: javascript html dom iframe phantomjs