Python.硒. drag_and_drop错误'AttributeError:move_to需要WebElement' [英] Python. Selenium. drag_and_drop error 'AttributeError: move_to requires a WebElement'

查看:1381
本文介绍了Python.硒. drag_and_drop错误'AttributeError:move_to需要WebElement'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,我做错了什么? 我尝试拖放Selenium,但是每次遇到错误"AttributeError:move_to需要WebElement"

Tell me please, what am I doing wrong? I try to drag and drop through Selenium, but every time I come across an error "AttributeError: move_to requires a WebElement"

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

chromedriver = '/usr/local/bin/chromedriver'

driver = webdriver.Chrome(chromedriver)
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')

source = driver.find_elements_by_xpath('//*[@id="box3"]')
target = driver.find_elements_by_xpath('//*[@id="box103"]')

action = ActionChains(driver)
action.drag_and_drop(source, target).perform()

我也这样尝试过:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

chromedriver = '/usr/local/bin/chromedriver'

driver = webdriver.Chrome(chromedriver)
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')

source = driver.find_elements_by_xpath('//*[@id="box3"]')
target = driver.find_elements_by_xpath('//*[@id="box103"]')
ActionChains(driver).click_and_hold(source).move_to_element(target).release(target).perform()

总是出现"AttributeError:move_to需要一个WebElement"

Traceback (most recent call last):
  File "drag_and_drop_test.py", line 13, in <module>
    ActionChains(driver).click_and_hold(source).move_to_element(target).release(target).perform()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/action_chains.py", line 121, in click_and_hold
    self.move_to_element(on_element)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/action_chains.py", line 273, in move_to_element
    self.w3c_actions.pointer_action.move_to(to_element)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/actions/pointer_actions.py", line 42, in move_to
    raise AttributeError("move_to requires a WebElement")
AttributeError: move_to requires a WebElement

推荐答案

find_elements_by_xpath返回WebElement的列表,drag_and_drop(和其他方法)接受单个WebElement.使用find_element_by_xpath

find_elements_by_xpath returns a list of WebElements, drag_and_drop (and the other methods) accept a single WebElement. Use find_element_by_xpath

source = driver.find_element_by_xpath('//*[@id="box3"]')
target = driver.find_element_by_xpath('//*[@id="box103"]')

这篇关于Python.硒. drag_and_drop错误'AttributeError:move_to需要WebElement'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆