gpt4 book ai didi

Python。 Selenium 。拖放错误 'AttributeError: move_to requires a WebElement'

转载 作者:行者123 更新时间:2023-12-03 20:26:54 25 4
gpt4 key购买 nike

请告诉我,我做错了什么?
我尝试通过 Selenium 拖放,但每次遇到错误“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 的列表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"]')

关于Python。 Selenium 。拖放错误 'AttributeError: move_to requires a WebElement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com