Home > Back-end >  Selecting Element from dropdown with style="display: none" doesn't work in Selenium
Selecting Element from dropdown with style="display: none" doesn't work in Selenium

Time:08-04

<div  tabindex="-1"><div ><input id="TextID_Search"  type="text" role="search" autocomplete="off" style="width: 205.2px;" aria-label="* Typ:"></div></div>
<select  id="TextID" name="TextID" style="display: none;" aria-required="true">
  <option value="">-</option>
  <option value="1">Text1</option>
  <option value="2">Text2</option>
  <option value="3">Text3</option>
  <option value="4">Text4</option>
  <option value="5">Text5</option>
</select>

In the dropdown I'd like to select 'Text3', but it doesn't work. What I tried:

driver.find_element("id", "TextID_Search").send_keys("Text3")
driver.send_keys(Keys.DOWN)
driver.send_keys(Keys.RETURN)

and as well:

mySelectElement = browser.find_element_by_id('TextID')
dropDownMenu = Select(mySelectElement)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "TextID")))
dropDownMenu.select_by_visible_text('Text3')

I'm stuck and don't know how to solve this. Need some help, thanks!

CodePudding user response:

With Selenium, AFAIK, you cannot select elements with display:none, if you want to force it, you'll need to change the display property to something visible.

driver.execute_script("document.getElementById('TextID').style.display='block';")
// Your code to select and operate the Element by ID

An example about how to execute JS scritps from Selenium: https://pythonbasics.org/selenium-execute-javascript/

  • Related