Search code examples
python-3.xselenium-chromedriverdrag-and-drop

how to perform left click and drag to right slide button with selenium in python


How can I perform a left click and drag to right slide (with approximately 1 second transition time) with selenium in python.

Before drag

enter image description here

<div style="display:inline-block;" tabindex="11" class="slide-submit" id="continueId">
    <button type="submit" class="ui-draggable ui-draggable-handle" style="left: 0px; top: 0px;">move me<span class="arrow-right"> →</span></button>
    <label>drag to continue</label>
</div>

After drag

enter image description here

<div style="display:inline-block;" tabindex="11" class="slide-submit" id="continueId">
    <button type="submit" class="ui-draggable ui-draggable-handle ui-draggable-dragging" style="left: 123px; top: 0px;">move me<span class="arrow-right"> →</span></button>
    <label>drag to continue</label>
</div>

python: 3.11.1, ChromeDriver: 109.0.5414.74, Selenium: 4.8.0


Solution

  • Have you tried action_chains

    import

    from selenium.webdriver.common.action_chains import ActionChains
    

    Element to drag

    drag_element = driver.find_element_by_class_name("ui-draggable")
    

    Perform the drag

    action = ActionChains(driver)
    action.click_and_hold(drag_element)
    action.move_by_offset(125, 0)
    action.release().perform()
    

    Note: Try the offset X and Y according to need.