拖放操作不适用于Selenium python

2024-05-05 18:34:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用selenium在网站中进行简单的拖放操作,但是它不起作用,我也不知道为什么

网站:(https://app.orcatec.com/matrix

我的代码:

driver = webdriver.Firefox()
driver.get("https://app.orcatec.com/login")

time.sleep(2)

email = driver.find_element_by_id("login-email")
email.send_keys("xxxxxxx")

passw = driver.find_element_by_id("login-password")
passw.send_keys("xxxxxxxx")

login = driver.find_element_by_class_name("btn-primary")
login.click()

time.sleep(6)

那就是登录

要拖放,请执行以下操作:

app_square = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/div[4]/div/div/div/div/div/div/div[2]/div[2]/div[2]/div[2]/div[1]/div/div/span/div[2]")
action = webdriver.common.action_chains.ActionChains(driver)
empty_square = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/div[4]/div/div/div/div/div/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[3]")
time.sleep(1)
action.move_to_element(app_square).pause(1).click_and_hold().move_to_element(empty_square).context_click().perform()

请让我知道我做错了什么。我还尝试了使用selenium进行拖放的各种其他方法,例如拖放方法


2条回答

使用python的pyautogui库解决了此问题

代码:

pyautogui.moveTo(2485, 373)
pyautogui.drag(-330, 80)
pyautogui.moveTo(2456, 458)
pyautogui.moveTo(2156, 458)
pyautogui.click()

一个基本的方法是使用ActionChain drag_and_drop

ActionChains(driver).drag_and_drop(app_square, empty_square).pause(5).perform()

阅读更多here

drag_and_drop(source, target)
Holds down the left mouse button on the source element,
then moves to the target element and releases the mouse button.
Args:   
source: The element to mouse down.
target: The element to mouse up.

相关问题 更多 >