我正在使用 phantomjs(1.5) 和casperjs http://casperjs.org/用于我的功能测试。
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 的最新版本允许我们使用--网络安全=否标记为不尊重安全政策。
下一个脚本(仅限 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=无 iframe.jshttp://anysite.org http://anysite.org
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)