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.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
|