拖放在Selenium python上不起作用



我试图在一个网站内使用硒做一个简单的拖放,但它不工作,我不知道为什么。

网站(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的各种其他拖放方法,例如drag_and_drop方法。

使用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()

点击这里阅读更多

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.

最新更新