我正在使用selenium(最新版本)和python 2.7.8。尝试使用jquery ui拖放到<div id="droppable">
时,我收到以下错误消息。 TypeError: unbound method move_to_element() must be called with ActionChains ins
tance as first argument (got list instance instead)
实际上,我的代码是在jQuery UI网站的可放置页面上测试它。我正在使用的代码如下所示:
def testStep4(self):
# Dragging and dropping a page element
self.driver.switch_to_frame(self.driver.find_element_by_tag_name("iframe"))
self.driver.implicitly_wait(15)
element = self.driver.find_elements_by_id("droppable")
action_chains.ActionChains.move_to_element(element)
我试图操纵的jQuery UI代码是:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
#droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
</body>
</html>
任何帮助都将非常感激。我只是困惑。我正在使用drag_and_drop
selenium对象,但它返回了相同的确切错误消息。我觉得我越来越接近这个问题了。谢谢!
答案 0 :(得分:1)
首先,使用单数形式find_element_by_id
,这样您只能获得一个元素,而不是列表。在您的情况下,我认为没有理由使用find_elements_by_id
。然后,您需要实例化ActionChains
对象并在其上调用move_to_element
,然后perform
让它执行操作。所以像这样:
element = self.driver.find_element_by_id("droppable")
action_chains.ActionChains(driver) \
.move_to_element(element) \
.perform()
如果你想要一次完成拖放,你应该这样做:
target = self.driver.find_element_by_id("droppable")
source = self.driver.find_element_by_id("draggable")
action_chains.ActionChains(driver) \
.drag_and_drop(source, target) \
.perform()