2015-04-01 16 views
9

Sto usando Python 2.7 e Selenium 2.44.Come simulare il trascinamento e il rilascio di HTML5 in selenio Webdriver?

voglio automatizzare trascinare e rilasciare azione selenio WD ma secondo altri post correlati azioni in HTML5 non sono supportati da Selenio ancora. C'è un modo per simulare il drag and drop in Python?

Ecco il codice che ho provato:

driver = webdriver.Firefox() 
driver.get("http://html5demos.com/drag") 
target = driver.find_element_by_id("one") 
source = driver.find_element_by_id("bin") 
actionChains = ActionChains(driver) 
actionChains.drag_and_drop(target, source).perform() 

e non ha funzionato.

risposta

20

Sì, HTML5 "drag & drop" non è attualmente supportata da Selenio:

Uno dei suggested workarounds è di simulare HTML5 trascinare e rilasciare tramite JavaScript:

  • scaricare drag_and_drop_helper.js
  • eseguire lo script tramite execute_script() chiamando simulateDragDrop() funzione su un elemento source passando l'elemento target come codice dropTarget

Esempio:

with open("drag_and_drop_helper.js") as f: 
    js = f.read() 

driver.execute_script(js + "$('#one').simulateDragDrop({ dropTarget: '#bin'});") 

Il problema è che non funzionerà in il tuo caso "così com'è" dal momento che lo richiede jQuery.


Ora abbiamo bisogno di capire come caricare dinamicamente jQuery. Per fortuna, there is a solution.

completo esempio di lavoro in Python:

from selenium import webdriver 

jquery_url = "http://code.jquery.com/jquery-1.11.2.min.js" 

driver = webdriver.Firefox() 
driver.get("http://html5demos.com/drag") 
driver.set_script_timeout(30) 

# load jQuery helper 
with open("jquery_load_helper.js") as f: 
    load_jquery_js = f.read() 

# load drag and drop helper 
with open("drag_and_drop_helper.js") as f: 
    drag_and_drop_js = f.read() 

# load jQuery 
driver.execute_async_script(load_jquery_js, jquery_url) 

# perform drag&drop 
driver.execute_script(drag_and_drop_js + "$('#one').simulateDragDrop({ dropTarget: '#bin'});") 

dove jquery_load_helper.js contiene:

/** dynamically load jQuery */ 
(function(jqueryUrl, callback) { 
    if (typeof jqueryUrl != 'string') { 
     jqueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'; 
    } 
    if (typeof jQuery == 'undefined') { 
     var script = document.createElement('script'); 
     var head = document.getElementsByTagName('head')[0]; 
     var done = false; 
     script.onload = script.onreadystatechange = (function() { 
      if (!done && (!this.readyState || this.readyState == 'loaded' 
        || this.readyState == 'complete')) { 
       done = true; 
       script.onload = script.onreadystatechange = null; 
       head.removeChild(script); 
       callback(); 
      } 
     }); 
     script.src = jqueryUrl; 
     head.appendChild(script); 
    } 
    else { 
     callback(); 
    } 
})(arguments[0], arguments[arguments.length - 1]); 

Prima/Dopo risultato:

+0

Grazie mille @alecxe – Mahmor

+0

Il tuo esempio è ancora funzionante. Sono riuscito a tradurlo e usarlo in C#. Grazie per l'aiuto! –

+5

Ho convertito questo helper in JS nativo, eliminando la necessità di iniettare jQuery: https://gist.github.com/druska/624501b7209a74040175 – Druska

Problemi correlati