0

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

BrianChe
  • 1
  • 1
  • 4

2 Answers2

1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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()
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • Tried that and still getting the error, Element could not be scrolled into view. Also edited original post to add that I switch to iframe before selecting – BrianChe Mar 30 '20 at 11:49
  • use Action class/ javascript to move on to element and then select a value from drop-down – SeleniumUser Mar 30 '20 at 11:52