0

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.

ShridharK
  • 365
  • 2
  • 14
  • After selecting, I also want to click on that element. – ShridharK May 31 '21 at 14:47
  • You can find an answer to this question here: [https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python](https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python) – Cédric May 31 '21 at 14:49
  • You can find an answer to this question here: https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python – Cédric May 31 '21 at 14:50
  • If you look at the 2nd answer in your link, I've done exactly that but it still isn't working – ShridharK May 31 '21 at 14:50
  • I've added the answer :) – ShridharK May 31 '21 at 15:39

3 Answers3

1

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()
ShridharK
  • 365
  • 2
  • 14
0

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.
JorgeHB
  • 103
  • 8
  • Can you please fill in the blanks for my code, it'll be really helpful. I'm finding the syntax of that confusing for now – ShridharK May 31 '21 at 14:54
  • Edited! Tell me if it works just with this :) – JorgeHB May 31 '21 at 15:03
  • 1
    The problem is that the element is present but not visible when the interaction is attempted. So I don't see how waiting for the presence will help. Perhaps you should use `EC.element_to_be_visible()`? – C. Peck May 31 '21 at 15:05
  • Yes, it is more logical your argument. – JorgeHB May 31 '21 at 15:06
  • Should this be before 'mySelect'? Because I tried that and now getting this error: AttributeError: type object 'By' has no attribute 'value' – ShridharK May 31 '21 at 15:07
  • Just in case the problem can be resolved with this, first use time.sleep(10) after defining mySelect. It is more or less like the WebDriverWait but in a less fancy way. If it doesn't work with time.sleep() then the problem is another thing – JorgeHB May 31 '21 at 15:13
  • I talked to my colleague and he said that it's because the the display style is none. So I have to somehow make that visible? Do you know how to do that? At the end in html, you'll see 'style: display none' – ShridharK May 31 '21 at 15:23
  • I've added the answer :) – ShridharK May 31 '21 at 15:39
0

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')
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • For the 1st piece of code, I'm getting this error: AttributeError: move_to requires a WebElement – ShridharK May 31 '21 at 15:00
  • For the 2nd piece of code, I'm getting this error: AttributeError: 'Select' object has no attribute 'id' – ShridharK May 31 '21 at 15:03
  • This is what YOU wrote. I have no idea how to locate that element – Prophet May 31 '21 at 15:07
  • It's your code `mySelect = Select(driver.find_element_by_id("childContextDDL"))` – Prophet May 31 '21 at 15:08
  • Yes, but I'm taking that id from html. Don't know why I'm getting that error – ShridharK May 31 '21 at 15:09
  • Can you please provide a link to the website? – Prophet May 31 '21 at 15:12
  • Sorry, it's a confidential company website where you won't be able to log in. I talked to my colleague and he said that it's because the the display style is none. So I have to somehow make that visible? Do you know how to do that? – ShridharK May 31 '21 at 15:21
  • At the end in html you'll see 'style: display none' – ShridharK May 31 '21 at 15:21
  • I understand. It should be some user action that makes this element visible. Possibly tap on it, click on it, hover over it or some other element. maybe scroll to that element. – Prophet May 31 '21 at 15:23
  • I've added the answer :) – ShridharK May 31 '21 at 15:39
  • Congrats! But you have to admit that we couldn't guess this from your initial question details – Prophet May 31 '21 at 15:43
  • 1
    Haha it was still within the HTML code that I had posted but it was at the end. Anyway, it's fine, thanks for trying to help me out, really appreciate it! – ShridharK Jun 01 '21 at 06:00
  • 1
    I've asked 1 more question. If you have time, it will be helpful if you can look into it: https://stackoverflow.com/questions/67784552/unable-to-click-on-an-ahref-link-using-python-selenium – ShridharK Jun 01 '21 at 07:35