TypeError: unbound method move_to_element() must be called with ActionChains ins tance as first argument (got list instance instead)

TypeError: unbound method move_to_element() must be called with ActionChains ins tance as first argument (got list instance instead)

我正在使用 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 对象,但它返回了完全相同的错误消息。我觉得我离解决这个问题越来越近了。谢谢!

首先,使用单数形式 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()