【问题标题】:Accessing the contentDocument of an iframe in phantomjs在 phantomjs 中访问 iframe 的 contentDocument
【发布时间】: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


【解决方案1】:

我遇到了这个问题,但发现这是因为我没有将代码包装在评估()中。不过,你似乎正在这样做。试试这个不使用 jquery。

page.evaluate(function (){
      iframe = document.getElementById('iframeName').contentDocument
      iframe.getElementById("testInput").value = "test";
});

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 2014-10-27
    • 2016-04-12
    • 2023-03-26
    • 2013-04-29
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多