【问题标题】:cannot perform drag-n-drop python selenium无法执行拖放 python selenium
【发布时间】:2019-09-16 12:58:27
【问题描述】:

我在 selenium 中创建了一个测试并将其导出到 python 中。该测试包括将元素拖放到 div。当我在 python 中运行测试时,无法执行拖放操作。我使用的代码是:

source_element =  self.driver.find_elements_by_xpath("//*[contains(@id, 
'plinkTest')]")
 dest_element = self.driver.find_elements_by_xpath("//* 
 [@id='tools_dep_jstree_id']")

actions = ActionChains(self.driver)
actions.move_to_element(source_element).click_and_hold().perform()
actions = ActionChains(self.driver)

actions.move_to_element(dest_element).perform()
actions = ActionChains(self.driver)
actions.move_to_element(dest_element).release().perform()

我得到的错误是“move_to 需要一个 web 元素”。

【问题讨论】:

  • 请提供相关的HTML?

标签: python selenium drag-and-drop


【解决方案1】:

find_elements_by_xpath() 将返回为 list 而不是 WebElement

find_elements_by_xpath()

更改以下行
 source_element =  self.driver.find_elements_by_xpath("//*[contains(@id, 
'plinkTest')]")
 dest_element = self.driver.find_elements_by_xpath("//* 
 [@id='tools_dep_jstree_id']")

find_element_by_xpath()

 source_element =  self.driver.find_element_by_xpath("//*[contains(@id, 
'plinkTest')]")
 dest_element = self.driver.find_element_by_xpath("//* 
 [@id='tools_dep_jstree_id']")

【讨论】:

    【解决方案2】:

    问题是使用 find_elements_by_xpath 而不是 find_element_by_xpath (第一个返回列表,只是拼写错误)并且只能通过使用链接文本找到源元素:

    source_element = self.driver.find_element_by_partial_link_text('plink')
    dest_element = self.driver.find_element_by_xpath("//*[@id='tools_dep_jstree_id']/ul")
    
    actions = ActionChains(self.driver)
    actions.move_to_element(source_element).click_and_hold().perform()
    
    time.sleep(2)
    actions = ActionChains(self.driver)
    actions.move_to_element(dest_element).perform()
    
    actions = ActionChains(self.driver)
    actions.move_to_element(dest_element).release().perform()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多