Skip to content
Advertisement

How can I slide input[type=text] using selenium in python?

enter image description here

I am scraping from a dynamic website and I need to slide this input to certain range. The HTML for this shows that it’s an input[type=’text] with display:none in disguise. After sliding, the text box is set to “{minValue}{maxValue}” based on the sliders’ positions.

<div class="slider slider-horizontal" id="" style="width: 100%;"><div class="slider-track"><div class="slider-track-low" style="left: 0px; width: 0%;"></div><div class="slider-selection" style="left: 0%; width: 100%;"></div><div class="slider-track-high" style="right: 0px; width: 0%;"></div></div><div class="tooltip tooltip-main top in" role="presentation" style="left: 50%; margin-left: -39.5px;"><div class="tooltip-arrow"></div><div class="tooltip-inner">2006 : 2022</div></div><div class="tooltip tooltip-min top" role="presentation" style="left: 0%; margin-left: 0px; display: none;"><div class="tooltip-arrow"></div><div class="tooltip-inner">2006</div></div><div class="tooltip tooltip-max top" role="presentation" style="left: 100%; margin-left: 0px; display: none;"><div class="tooltip-arrow"></div><div class="tooltip-inner">2022</div></div><div class="slider-handle min-slider-handle round" role="slider" aria-valuemin="2006" aria-valuemax="2022" aria-valuenow="2006" tabindex="0" style="left: 0%;"></div><div class="slider-handle max-slider-handle round" role="slider" aria-valuemin="2006" aria-valuemax="2022" aria-valuenow="2022" tabindex="0" style="left: 100%;"></div></div>

I can’t set its values anyhow. I have tried:

  • changing the display to block and then editing the textbox to the required values.
  • changing the aria-valuenow attribute by executing js
  • changing the value attributes for the textbox.
  driver.execute_script(
        'document.getElementsByClassName("min-slider-handle")[0].setAttribute("aria-valuenow", 2019);'
    )
  hidden_element = driver.find_element(By.CSS_SELECTOR, "#ID .slider-input")
    hidden_element[0].send_keys(f'{from_year}, {to_year}')

It seems that the apply button that is clicked after sliding to the desired locations derives the min and max from the slider positions instead of any attributes. How do I handle sliding this then?

Advertisement

Answer

I solved the issue using:

slider = driver.find_element(By.CSS_SELECTOR, ".min-slider-handle")
    move = ActionChains(driver)
    move.click_and_hold(slider).
move_by_offset((from_year - MIN_YEAR) * width * 1.0 / (MAX_YEAR - MIN_YEAR),
                                               0).
release().
perform()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement