2012-03-30 11 views
10

私の機能テストにはphantomjs(1.5)とcasperjsが使用されています。PhantomJSとiFrame

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() 

だから、iframeを搭載したvk.com(ロシアのソーシャルネットワーク)にログインします。

どのようにiFrameで要素を使用できますか?たとえば、ボタンをクリックしますか?

答えて

11

最近のバージョンの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(); 
}); 

=なしiframe.js http://anysite.org

+0

DudeSweet、このコードは2013年に書かれ、その時点でidのaswift_2とgoogle_ads_frame3がadsenseに存在していたが、コードの作業は数年後には変わっていないというのは少し不合理なことだ(ページがHTML要素いつでも)。 私の答えの中で最も重要なのは、 "--web-security = no"オプションとiframeにアクセスするための小さなjavascriptロジックを使用することです。 –

-3

アプリケーションがiframe内のドメインと異なるドメインにある場合、スクリプトはiframeのコンテンツと対話できません。参照:Can scripts in iframe interact with scripts in the main page

+14

が、ファントムのブラウザではなく、スクリプトです...ブラウザがあってはならない--webセキュリティパラメータで

phantomjsを実行して、覚えておいてくださいドメインに関係なくページとやり取りできますか? – gcb