【问题标题】:Testing elements inside unfriendly iframes with PhantomJS使用 PhantomJS 在不友好的 iframe 中测试元素
【发布时间】:2016-01-08 12:28:37
【问题描述】:

有谁知道是否可以在 phantomjs 内不友好的 iframe 中测试 dom 元素?如果可能,有人会怎么做?

我知道我们需要将 webSecurity 设置为 false,但除此之外,我对 iframe 没有任何经验。

'web-security': false

访问 iframe 元素:

"use strict";
var page = require('webpage').create();

page.open('url', function (status) {
   var _count = page.evaluate(function() {
        var matches = document.querySelectorAll('iframe');
        var count = 0;
        [].forEach.call(matches, function(match) {
            var add = 0;
            try {
                add = match.contentDocument.querySelectorAll('div').length;
            } catch (x) {

            }
            count += add;
        });

        return count;
    });

    console.log(_count); // 0 - not true
    phantom.exit();

});

运行 PhantomJS

 npm run phantomjs --web-security=false server/tests/unf.js 

【问题讨论】:

  • 你说的“不友好”是什么意思?您可以切换到您想要的框架并在那里做任何您想做的事情。您不必禁用 webSecurity 来执行此操作。 phantomjs.org/api/webpage/method/switch-to-frame.html
  • 跨域,iframe有src。
  • 那有什么问题呢?你能举一个像标记这样的框架的例子,以及你是如何尝试访问它的吗?
  • 你知道documentevaluate()里面只有一些意义吗?您不能使用它来切换到框架。

标签: javascript testing iframe phantomjs


【解决方案1】:

不要运行本地 phantomjs,phantomjs 必须全局安装。

npm install phantomjs -g
phantomjs --web-security=false server/tests/unf.js 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多