Home > Back-end >  How can I slide input[type=text] using selenium in python?
How can I slide input[type=text] using selenium in python?

Time:07-09

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  id="" style="width: 100%;"><div ><div  style="left: 0px; width: 0%;"></div><div  style="left: 0%; width: 100%;"></div><div  style="right: 0px; width: 0%;"></div></div><div  role="presentation" style="left: 50%; margin-left: -39.5px;"><div ></div><div >2006 : 2022</div></div><div  role="presentation" style="left: 0%; margin-left: 0px; display: none;"><div ></div><div >2006</div></div><div  role="presentation" style="left: 100%; margin-left: 0px; display: none;"><div ></div><div >2022</div></div><div  role="slider" aria-valuemin="2006" aria-valuemax="2022" aria-valuenow="2006" tabindex="0" style="left: 0%;"></div><div  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?

CodePudding user response:

You can drag the min and max controls using action chain and then get the value from text box. this may help you.

CodePudding user response:

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