javascript – PhantomJS和iFrame

我使用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中使用元素,例如点击按钮?

解决方法

最近版本的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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...