Search code examples
pythonselenium-webdriverweb-scrapingslider

so difficult to slide slider to correct position


theres a slider i need to slide until it has a certain value. i find it very difficult to slide to the correct value.

any offset less than 6 seems not to do anything

anything over 5 seems to just move quite randomly

heres the code im using to try to get to the right value

while miles_value.get_attribute('innerHTML').replace(",","").strip() != str(mil):
    if int(miles_value.get_attribute('innerHTML').replace(",","").strip()) < mil:
        move.click_and_hold(miles_slider).move_by_offset(6, 0).release().perform()
        print("were les than goal so offsetting positivley")
    else:
        move.click_and_hold(miles_slider).move_by_offset(-7, 0).release().perform()
        print("were more than goal so offsetting negativly")
    print("g4.5")
    print(miles_slider.get_attribute('aria-valuenow'))
    print(str(miles_value.get_attribute('innerHTML').replace(",","")).strip()+" != "+str(mil))

this give me an output of

20000 != 25000
were les than goal so offsetting positivley
g4.5
35.3
22500 != 25000
were les than goal so offsetting positivley
g4.5
47.1
27500 != 25000
were more than goal so offsetting negativly
g4.5
52.9
30000 != 25000
were more than goal so offsetting negativly
g4.5
52.9
30000 != 25000
were more than goal so offsetting negativly
g4.5
47.1
27500 != 25000
were more than goal so offsetting negativly
g4.5
35.3

....

and can take over 5 minutes to get the right value

does anyone have any advice for this type of thing?

just for context im aiming for the miles slider on this site

https://www.nationwidevehiclecontracts.co.uk/car-leasing/abarth/500e/hatchback/114kw-scorpionissima-42-2kwh-auto1


Solution

  • Try below code , I have Tested it at multiple sizes and allowed values worked fine for me like

    Using an offset of 9 for positive cases and -9 for negative cases if there is one, also added the locators as needed

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    
    driver = webdriver.Chrome()
    driver.set_window_position(0, 0)
    driver.set_window_size(1400, 1500)
    driver.get(
        'https://www.nationwidevehiclecontracts.co.uk/car-leasing/abarth/500e/hatchback/114kw-scorpionissima-42-2kwh-auto1')
    mil = 25000
    move = ActionChains(driver)
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@id='slider-non-linear-value']")))
    miles_value = driver.find_element(By.XPATH, "//span[@id='slider-non-linear-value']")
    driver.execute_script("arguments[0].scrollIntoView()", miles_value)
    miles_slider = driver.find_element(By.XPATH, '//div[@class="noUi-handle noUi-handle-lower"]')
    while miles_value.get_attribute('innerHTML').replace(",", "").strip() != str(mil):
        if int(miles_value.get_attribute('innerHTML').replace(",", "").strip()) < mil:
            move.click_and_hold(miles_slider).move_by_offset(9, 0).release().perform()
            print("were les than goal so offsetting positivley")
        else:
            move.click_and_hold(miles_slider).move_by_offset(-9, 0).release().perform()
            print("were more than goal so offsetting negativly")
        print("g4.5")
        print(miles_slider.get_attribute('aria-valuenow'))
        print(str(miles_value.get_attribute('innerHTML').replace(",", "")).strip() + " != " + str(mil))