2012-03-30 16 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() 

तो, मैं (रूस में सामाजिक नेटवर्क) vk.com के लिए लॉग इन, मेरे ऐप आइफ्रेम के साथ भरी हुई।

मैं कैसे iFrame में तत्वों का उपयोग कर सकते हैं, उदाहरण के लिए एक बटन के लिए क्लिक करें?

उत्तर

11

फ़ैंटॉमजेएस के हाल के संस्करण हमें --web-security = no सुरक्षा नीति का सम्मान करने के लिए ध्वज का उपयोग करने की अनुमति देते हैं।

अगली लिपि (केवल फ़ैंटॉमजेएस) को आईफ्रेम, एक आईफ्रेम (ऐडसेंस) में एक लिंक का शीर्षक मिलता है।

/* 
    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-सुरक्षा = नहीं iframe.js http://anysite.org

+0

DudeSweet, इस कोड को 2013 में लिखा गया था, और उस समय, आईडी के aswift_2 और google_ads_frame3 ऐडसेंस में ही अस्तित्व में, यह एक छोटे विसंगत कि कोड परिवर्तन wihout कुछ साल (यह सोच कर कि एक पेज अपने html तत्वों की आईडी को बदल सकते हैं के बाद काम है किसी भी समय)। सबसे अपने जवाब के महत्वपूर्ण, विकल्प "--web-सुरक्षा = नहीं" और iframe उपयोग करने के लिए कुछ छोटे जावास्क्रिप्ट तर्क का प्रयोग है। –

-3

यदि आपका एप्लिकेशन iframe में से किसी एक से भिन्न डोमेन पर है, तो आपकी स्क्रिप्ट आईफ्रेम की सामग्री से सहभागिता नहीं कर सकती है। देखें: Can scripts in iframe interact with scripts in the main page

+14

लेकिन प्रेत ब्राउज़र, नहीं एक स्क्रिप्ट है पैरामीटर के साथ चलाने के ... ब्राउज़र नहीं होना चाहिए डोमेन की परवाह किए बिना एक पेज के साथ बातचीत करने के लिए एक सक्षम? – gcb