如何在Selenium Webdriver中模拟HTML5拖放?

时间:2015-04-01 01:11:29

标签: python html5 selenium selenium-webdriver drag-and-drop

我正在使用Python 2.7和Selenium 2.44。

我希望在Selenium WD中自动拖放操作,但根据其他相关帖子,不支持Selenium 中的HTML5操作。有没有办法在Python中模拟拖放?

这是我尝试过的代码:

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

它不起作用。

3 个答案:

答案 0 :(得分:24)

是的,HTML5"拖放" Selenium目前不支持

其中一个suggested workarounds通过JavaScript模拟HTML5拖放

  • 下载drag_and_drop_helper.js
  • 通过execute_script()simulateDragDrop()元素上调用source函数执行脚本,将target元素作为dropTarget
  • 传递

示例代码:

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

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

问题是它不会在你的情况下工作"因为"因为需要jQuery


现在我们需要找出如何动态加载jQuery 。谢天谢地,there is a solution

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'});")

jquery_load_helper.js包含:

/** 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]);

结果之前/之后:

答案 1 :(得分:0)

答案 2 :(得分:0)

在python中找到了工作示例(不使用jquery_load_helper.js)-https://gist.github.com/rcorreia/2362544#gistcomment-2708388

积分:Kelanmomo

如alecxe所述:下载drag_and_drop_helper.js

# coding = utf-8
from selenium import webdriver
import os
from time import sleep

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://the-internet.herokuapp.com/drag_and_drop')

with open(os.path.abspath('drag_and_drop_helper.js'), 'r') as js_file:
    line = js_file.readline()
    script = ''
    while line:
        script += line 
        line = js_file.readline()

driver.execute_script(script + "$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});")
sleep(2)
driver.quit()