请告诉我,我做错了什么? 我尝试在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
答案 0 :(得分:2)
find_elements_by_xpath
返回一个WebElement
的列表,drag_and_drop
(和其他方法)接受单个WebElement
。使用find_element_by_xpath
source = driver.find_element_by_xpath('//*[@id="box3"]')
target = driver.find_element_by_xpath('//*[@id="box103"]')
答案 1 :(得分:2)
@guy所说:
find_elements_by_xpath
返回WebElements
的列表。您可以使用find_element_by_xpath
方法来获取单个Web元素。或从WebElements
返回的find_elements_by_xpath
中选择特定元素。例如,如果您知道,则想从返回列表中为目标选择第二个元素。然后您可以尝试这样:
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"]')[0]
target = driver.find_elements_by_xpath('//*[@id="box103"]')[1]
action = ActionChains(driver)
action.drag_and_drop(source, target).perform()
我可以看到我们正在选择具有id的元素,但是id是唯一的,因此只能有一个id。因此,您也可以这样:
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_element_by_id('box3')
target = driver.find_element_by_id('box103')
action = ActionChains(driver)
action.drag_and_drop(source, target).perform()
我喜欢使用find_element_by_id
,因为它对我来说比xpath干净。
答案 2 :(得分:0)
使用 find_elements_by_xpath
代替 find_element_by_xpath
答案 3 :(得分:-1)
此错误消息...
AttributeError: move_to requires a WebElement
...表示move_to_element()
需要一个 WebElement 作为参数。
您似乎很近。您已使用find_elements_by_xpath()
返回了 List ,并根据需要在move_to_element()
中传递了 WebElement 。
在文本为美国的元素内拖动文本为华盛顿的元素,并 drop Selenium您必须为element_to_be_clickable()
引入 WebDriverWait ,并且可以使用以下Locator Strategies:
代码块:
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
source = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dragableBox' and @id='box3']")))
target = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dragableBoxRight' and @id='box103']")))
ActionChains(driver).click_and_hold(source).move_to_element(target).release(target).perform()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照: