1

I can't make Select() method working in selenium. I have this code:

<select class="print-select menuo selectBox" name="data[gimimo-m]" 
style="display: none;">
<option class="dis-option" value="">Mėnuo</option>
<option value="1">Sausis</option>
<option value="2">Vasaris</option>
<span class="selectBox-arrow-tip"/>
</select>
<a class="selectBox print-select menuo selectBox-dropdown" style="display:
inline-block; -moz-user-select: none;" title="" tabindex="0">
<span class="selectBox-label">Mėnuo</span>
<span class="selectBox-arrow"/>
<span class="selectBox-arrow-tip"/>
</a>

I use this line to select:

select_m = Select(driver.find_element_by_name('data[gimimo-m]'))
select_m.select_by_value("1")

I get this exception (exception is given when trying to select an option):

selenium.common.exceptions.ElementNotVisibleException:

I can click dropdown and see the options with this line:

select_button = driver.find_element_by_xpath("//a[@class='selectBox print-select menuo selectBox-dropdown']")
select_button.click()

After drop down is open code is changed to this:

<a class="selectBox print-select menuo selectBox-dropdown selectBox-menuShowing" 

But the select still stays invisible and i can't get the options:

<select class="print-select menuo selectBox" name="data[gimimo-m]" 
style="display: none;">

I get drop down opened but it results in the same exception. Can anyone help me to figure out what's wrong?

EDIT: SOLVED by this code:

select_month = driver.find_element_by_xpath("//a[@class='selectBox print-select menuo selectBox-dropdown']")
ActionChains(driver).move_to_element(select_month).click().send_keys('Kovas').send_keys(Keys.ENTER).perform()
1

The problem is with your style="display: none;"> attribute in HTML. Selenium only interacts with the elements that are visible on screen. So, you need to make your elements appear on the screen in order to make the elements interacted by Selenium code. Now, it depends on how your application is working so that you can write code for your next steps.

  1. If the drop down has elements appear after some time - (due to AJAX, or loading times) that would mean that the display:none attribute would disappear once elements are visible on screen. In this case you can use the waits in Selenium to wait for that period of time or to wait for the element to be present - please read about explicit waits on how to achieve the later.

    1. Or you can use javascript to click element directly without proceeding dropdown roll down. Js is able to cope with it. There are a lot of answers on SO regarding this. You can refer to one here, and use that in your code.
  • Problem is that i can't locate drop down option (as show in the example of your link). I can expand dropdown w/o problems but i can't locate the options. When i right click on an option in the dropdown, dropdown is just closed so i can't get the xpath of it. – Vadim Apr 11 '17 at 12:19
  • can you share the URL? – demouser123 Apr 11 '17 at 12:25
  • link Screenshot how to find the dropdown in the main topic. – Vadim Apr 11 '17 at 12:35
0

Exception definition and explanation from Selenium API documentation:

exception selenium.common.exceptions.ElementNotVisibleException(msg=None, screen=None)

Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with. Most commonly encountered when trying to click or read text of an element that is hidden from view.

You could use explicit wait, Xpath or robot class.

  • Waiting doesn't help here. I locate <select> element w/o problems. Problem here is selecting an potion which is hidden (i guess). – Vadim Apr 11 '17 at 9:14
  • You need to make it visible by following the same behaviour, gestures a user would. Find the dropdown, expand the list to make the items visible, then select the required option. – Martin Spamer Apr 11 '17 at 9:19
  • Actually i tried to do it by clicking the drop down expand button first: select_button = driver.find_element_by_xpath("//span[@class='selectBox-arrow']") select_button.click() – Vadim Apr 11 '17 at 9:24
  • The above line expands the drop down, but still i can't select the option for some reason. – Vadim Apr 11 '17 at 9:31
  • Please do not post unattributed text. I have edited to link to the source of the text and called out the quoted material. – Kate Paulk Apr 11 '17 at 17:42
0

Solved by using ActionChains:

select_month = driver.find_element_by_xpath("//a[@class='selectBox print-select menuo selectBox-dropdown']")
ActionChains(driver).move_to_element(select_month).click().send_keys('Kovas').send_keys(Keys.ENTER).perform()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.