【问题标题】:PhantomJS and iFramePhantomJS 和 iFrame
【发布时间】:2015-01-14 11:17:37
【问题描述】:

我正在使用 phantomjs(1.5) 和 casperjs 进行功能测试。

casper = require('casper').create
  loadImages: false


casper.start 'http://vk.com', ->
  @fill 'form[name="login"]', { email: mail, pass: pass}, true

casper.thenOpen "http://vk.com/#{app}", ->
  @echo "User at #{app}"  
casper.then ->
  @click "iframe['element']" #?! how I can do it?
casper.then ->
  @wait 2000000, -> @echo "exit from room: #{num}"


casper.run()

所以,我登录了 vk.com(俄罗斯的社交网络),我的应用加载了 iframe。

如何在 iFrame 中使用元素,例如单击按钮?

【问题讨论】:

    标签: javascript coffeescript phantomjs


    【解决方案1】:

    最新版本的 PhantomJS 允许我们使用 --web-security=no 标志来表示不遵守安全策略。

    下一个脚本(仅限 PhantomJS)获取 iframe 中链接的标题,即 iframe (adsense)。

    /*
        Accessing an iframe (different domain) with PhantomJS
        Example by deerme.org
    */
    
    var page = require('webpage').create(), system = require('system'), t, address;
    if (system.args.length === 1)
    {
        console.log('Usage: phantomfs iframe.js <some URL>');
        phantom.exit();
    }
    
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status)
    {
        if (status !== 'success')
        {
                console.log('FAIL to load the address');
        }
        else
        {
            t = (Date.now()) - t;
            title = page.evaluate( function(){
                return document.title;
            });
            linkTitle = page.evaluate( function(){
                // The site containing jQuery?
                if ( typeof(jQuery) == "undefined" )
                {
                    // Force Load
                    page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
                }
                // first iframe #aswift_2
                // second iframe #google_ads_frame3
                return jQuery("#aswift_2").contents()
                    .find("body")
                        .find("#google_ads_frame3")
                            .contents()
                                .find("body")
                                    .contents()
                                        .find("a:last")
                                            .attr("title");
            });
            console.log('Loading time: ' + t + ' msec');    
            console.log('Webpage title: ' + title);
            console.log('Link title (iframe adsense): ' + linkTitle);
        }
        phantom.exit();
    });
    

    记住,带参数运行

    phantomjs --web-security=no iframe.js http://anysite.org

    【讨论】:

    • DudeSweet,这段代码是2013年写的,当时adsense中存在id的aswift_2和google_ads_frame3,几年后代码工作没有变化有点不合逻辑(认为页面可以随时更改 html 元素的 id)。我最重要的答案是使用选项“--web-security=no”和一些小的 javascript 逻辑来访问 iframe。
    【解决方案2】:

    如果您的应用程序与 iframe 中的应用程序位于不同的域中,则您的脚本无法与 iframe 的内容进行交互。见:Can scripts in iframe interact with scripts in the main page

    【讨论】:

    • 但是幻象是浏览器,而不是脚本......浏览器不应该能够与页面交互而不管域吗?
    猜你喜欢
    • 2016-04-01
    • 1970-01-01
    • 2014-10-27
    • 2014-04-06
    • 1970-01-01
    • 2015-04-17
    • 2013-11-25
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多