【问题标题】:Drag and Drop not working on Selenium python拖放在 Selenium python 上不起作用
【发布时间】:2021-11-21 22:17:34
【问题描述】:

我正在尝试使用 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 进行拖放的方法,例如 drag_and_drop 方法。

【问题讨论】:

    标签: python selenium firefox


    【解决方案1】:

    使用 python 的 pyautogui 库解决了这个问题。

    代码:

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

    【讨论】:

    • 这不是 Selenium。
    【解决方案2】:

    一个基本的方法是拥有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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多