BrianChe
BrianChe

Reputation: 1

Selenium selecting from Dropdown Python

I'm using selenium in python and I'm looking to select the option Male from the below:

<div class="formelementcontent">
          <select aria-disabled="false" class="Width150" id="ctl00_Gender" name="ctl00$Gender" onchange="javascript: return doSearch();" style="display: none;">
           <option selected="selected" title="" value="">
           </option>
           <option title="Male" value="MALE">
            Male
           </option>
           <option title="Female" value="FEM">
            Female
           </option>
          </select>

Before selecting from the dropdown, I need to switch to iframe

driver.switch_to.frame(iframe)

I've tried many options and searched extensively. This gets me most of the way.

driver.find_element_by_id("ctl00_Gender-button").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ctl00_Gender")))
select=Select(driver.find_element_by_id("ctl00_Gender"))
check=select.select_by_visible_text('Male')

If I use WebDriverWait it times out.

I've tried selecting by visible text and index, both give:

ElementNotInteractableException: Element could not be scrolled into view

Upvotes: 0

Views: 3593

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193098

As per the HTML you have shared the <select> tag is having the value of style attribute set as display: none;. So using Selenium it would be tough interacting with this WebElement.

If you access the DOM Tree of the webpage through , presumably you will find a couple of <li> nodes equivalent to the <option> nodes within an <ul> node. You may be required to interact with those.

You can find find a couple of relevant detailed discussions in:

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

Try below solutions:

Solution 1:

select = Select(driver.find_element_by_id('ctl00_Gender'))
select.select_by_value('MALE')

Note add below imports to your solution :

from selenium.webdriver.support.ui import Select

Solution 2:

driver.find_element_by_xpath("//select[@id='ctl00_Gender']/option[text()='Male']").click()

Upvotes: 0

Related Questions