python - Python。硒。拖放错误“AttributeError:move_to 需要 WebElement”
问题描述
请告诉我,我做错了什么?我尝试通过 Selenium 拖放,但每次遇到错误“AttributeError:move_to 需要 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
s 列表,drag_and_drop
(和其他方法)接受一个WebElement
. 采用find_element_by_xpath
source = driver.find_element_by_xpath('//*[@id="box3"]')
target = driver.find_element_by_xpath('//*[@id="box103"]')
推荐阅读
- javascript - 为什么当我添加一个用于刷新(拉)Web 视图的标签时 ui 会发生变化?
- python - pip install --upgrade 排除文件列表被覆盖
- flutter - 如何在颤动中显示动画图像
- mysql - MYSQL 截断不正确的 DOUBLE 值:乘以
- django - python django url在交换时显示旧图像
- javascript - package.json 中使用 rollup.js 的 pkg.module (.esm.js) 有什么意义
- vue.js - v-model 在 select2 中不起作用。我不知道我该如何解决。当我删除 select2 的类 v-model 时
- android - 在 Flutter 中剪辑后删除多余的部分
- java - 如何解决 MediaPlayer SeekBar onStartTrackingTouch 中的 NullPointerException
- amazon-web-services - 如何更新 aws s3 更新文件下载文件名