Tak, HTML5 "drag & drop" nie jest obecnie obsługiwana przez Selen:
Jednym z suggested workarounds ma symulować HTML5 przeciągnij i upuść poprzez JavaScript:
- pobierz
drag_and_drop_helper.js
- wykonać skrypt poprzez
execute_script()
nazywając simulateDragDrop()
funkcji na source
elementu przechodzącego elementu target
jako kod dropTarget
Sample:
with open("drag_and_drop_helper.js") as f:
js = f.read()
driver.execute_script(js + "$('#one').simulateDragDrop({ dropTarget: '#bin'});")
Problemem jest to, że nie będzie działać w Wasz przypadek "taki, jaki jest", ponieważ wymaga wymaga jQuery
.
Teraz musimy dowiedzieć się jak dynamicznie załadować jQuery. Na szczęście, there is a solution.
Kompletny przykład pracujący w Pythonie:
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'});")
gdzie jquery_load_helper.js
zawiera:
/** 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]);
przed/po wyniku:
dziękuję @alecxe – Mahmor
swoim przykładzie nadal pracuje. Udało mi się go przetłumaczyć i użyć w języku C#. Dziękuję za pomoc! –
Przekonwertowałem tego pomocnika na macierzysty JS, usuwając potrzebę wstrzykiwania jQuery: https://gist.github.com/druska/624501b7209a74040175 – Druska