ShridharK
ShridharK

Reputation: 385

Unable to Click on Dropdown using Python Selenium

I'm trying to select an element from a dropdown but getting an error.

Here is the HTML:

<select id="childContextDDL" data-filter="contains" data-role="dropdownlist" data-template="dcf-context-picker" data-value-field="Value" data-text-field="DisplayText" data-bind="events: { change: childContextListChange }, source: childContextList.ChildContextList" style="display: none;">
<option value="1">NATION</option>
<option value="12">ATLANTIC</option>
<option value="16">CHARLOTTE, NC</option>

And this is the code that I'm trying to run:

mySelect = Select(driver.find_element_by_id("childContextDDL"))
print('MySelect is: ',mySelect)
mySelect.select_by_visible_text('ATLANTIC')

I'm getting this error:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated

What's the possible reason for getting this error? I'm very new to Selenium.

I also want to click on that element after selecting it.

Upvotes: 0

Views: 1140

Answers (3)

ShridharK
ShridharK

Reputation: 385

The problem was that the style within the html was set to none. So I had to first change that style to block to make it visible and then proceed with the clicking operation.

Here's the code that worked:

driver.execute_script("document.getElementById('childContextDDL').style.display = 'block';")

mySelect = Select(driver.find_element_by_id("childContextDDL"))
print('MySelect is: ',mySelect)
mySelect.select_by_visible_text('ATLANTIC')

randomClick = driver.find_element_by_id('dcf-user-info')
randomClick.click()

Upvotes: 1

JorgeHB
JorgeHB

Reputation: 103

If it says it is not currently visible, you should try pausing the code with one of this options:

time.sleep(1) #This states to your code to stop for 1 second and the continue with the work.
WebDriverWait(driver, 10).until(EC.element_to_be_visible(BY.value, "12")) # tells the driver to wait a maximum of ten seconds until the value "12" is visible for the driver.

Upvotes: 0

Prophet
Prophet

Reputation: 33361

I guess the mySelect element (the dropdown menu) is not visible.
So please try the following:

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)

mySelect = Select(driver.find_element_by_id("childContextDDL"))

actions.move_to_element(mySelect).perform()
mySelect.select_by_visible_text('ATLANTIC')

If the above doesn't work (I can't see the actual site you are working on), the following can work instead in the element has to be tapped to become enabled

action = TouchActions(driver)
action.tap(mySelect).perform()
mySelect.select_by_visible_text('ATLANTIC')

Upvotes: 0

Related Questions