2012-03-30 17 views
10

Estoy usando phantomjs (1.5) y casperjs para mis pruebas funcionales.PhantomJS y 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() 

Por lo tanto, me conecto a vk.com (red social en Rusia), mi aplicación cargada de marco flotante.

¿Cómo puedo usar elementos en iFrame, por ejemplo, hacer clic en un botón?

Respuesta

11

Las versiones recientes de PhantomJS nos permiten usar el --web-security = no marca por no respetar la política de seguridad.

El siguiente script (solo PhantomJS) obtiene el título de un enlace en el iframe, un 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(); 
}); 

Recuerde, ejecutar con el parámetro

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

+0

DudeSweet, este código fue escrito en 2013, y en ese momento, el ID aswift_2 y google_ads_frame3 existían en AdSense, es un poco ilógico que el código funcione sin cambios después de algunos años (pensando que una página puede cambiar la identificación de los elementos html en cualquier momento). la más importante de mis respuestas, es usar la opción "--web-security = no" y alguna pequeña lógica de JavaScript para acceder al iframe. –

-3

Si su aplicación está en un dominio diferente al del iframe, su secuencia de comandos no puede interactuar con los contenidos del iframe. Ver: Can scripts in iframe interact with scripts in the main page

+14

pero fantasma es el navegador, no un script ... no debe ser el navegador un capaz de interactuar con una página sin importar los dominios? – gcb