Home > Enterprise >  Drag and Drop not working on Selenium python
Drag and Drop not working on Selenium python

Time:09-30

I am trying to do a simple drag and drop within a website using selenium, however it is not working and I'm not sure why.

The website:(https://app.orcatec.com/matrix)

My code:

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)

That is to log in

To drag and drop:

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

Please let me know what I am doing wrong. I have also tried various other ways of dragging and dropping using selenium, such as the drag_and_drop method.

CodePudding user response:

Solved this problem using the pyautogui library for python.

Code:

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

CodePudding user response:

A basic way is to have ActionChain drag_and_drop

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

Read more 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.
  • Related