-1

I'm trying to move a slider slowly and smoothly with selenium but haven't found a way to do so.

https://jqueryui.com/slider/

ive found 2 methods of doing it, the first one moves it slow but not smoothly and the second one i was trying to use drag and drop to do it but it just moves it instantly.

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.chrome.options import Options
import os
from selenium.webdriver.common.action_chains import ActionChains
import math

options = Options()
options.add_argument('log-level=3')
driver = webdriver.Chrome(chrome_options=options, executable_path="/Users/andy_/Desktop/driver/chromedriver2.exe") # chrome_options=driver_options +
os.system('cls')


driver.get("https://jqueryui.com/slider/")

def move_slider(slider, offset):
    number = offset/20
    frac, whole = math.modf(number)
    dec = frac*20
    ActionChains(driver).click_and_hold(slider).perform()
    for i in range(20):
        ActionChains(driver).move_by_offset(number, 0).perform()
    ActionChains(driver).move_by_offset(dec, 0).perform()
    ActionChains(driver).release().perform()

def move_slider2(slider, offset):
    ActionChains(driver).drag_and_drop_by_offset(slider, 80, 0).perform()

sleep(1)

driver.switch_to.frame(0)
slider2 = driver.find_element(By.XPATH, "//span[@class='ui-slider-handle ui-corner-all ui-state-default']")
    
move_slider2(slider2, 80)
Andh
  • 1

1 Answers1

1

drag_and_drop_by_offset(source, xoffset, yoffset) holds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.


Solution

To slowly move the slider on the webpage with Selenium you can use the following solution:

  • Code block:

    driver.get('https://jqueryui.com/slider/')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='/resources/demos/slider/default.html']")))
    slider = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#slider > span")))
    ActionChains(driver).drag_and_drop_by_offset(slider, 80, 0).perform()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser snapshot:

slider

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352