import time
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
class Test_ActionChain():
def setup(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
def test_drag(self):
self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
drag_ele=self.driver.find_element(By.ID, "dragger")
drop_ele=self.driver.find_elements(By.XPATH, "/html/body/div[2]")
print(drop_ele1)
action = ActionChains(self.driver)
# action.drag_and_drop(drag_ele, drop_ele1).perform()
action.click_and_hold(drag_ele).move_to_element(drop_ele).release().perform()
time.sleep(3)
源码如上,执行拖拽操作,总是报如下错误
> def move_to(self, element, x=None, y=None):
> if not isinstance(element, WebElement):
> raise AttributeError("move_to requires a WebElement")
E AttributeError: move_to requires a WebElement,
后经仔细查看,是因为drop_ele=self.driver.find_elements(By.XPATH, “/html/body/div[2]”)多了一个“s”,因为:find_elements返回 WebElement 的列表s, drag_and_drop (和其他方法)接受单个 WebElement .所以应该使用find_element
本文档展示了在 Python 中使用 Selenium 进行拖放操作时遇到的问题。代码中,由于使用了 find_elements 导致元素集合被传递给 drag_and_drop 方法,而该方法需要单个 WebElement。修正方法是将 find_elements 改为 find_element。修复此错误后,拖放操作将能正确执行。
15万+

被折叠的 条评论
为什么被折叠?



