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()
asked Apr 11, 2017 at 8:50
3
  • stackoverflow.com/questions/27927964/… Commented Apr 11, 2017 at 9:05
  • style="display: none;" is the problem, how can you even click it when it isn't displayed? Commented Apr 11, 2017 at 9:59
  • How do i overcome the "style" problem? Commented Apr 11, 2017 at 10:02

3 Answers 3

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.
answered Apr 11, 2017 at 11:40
3
  • 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. Commented Apr 11, 2017 at 12:19
  • can you share the URL? Commented Apr 11, 2017 at 12:25
  • link Screenshot how to find the dropdown in the main topic. Commented Apr 11, 2017 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.

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Apr 11, 2017 at 9:11
5
  • Waiting doesn't help here. I locate <select> element w/o problems. Problem here is selecting an potion which is hidden (i guess). Commented Apr 11, 2017 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. Commented Apr 11, 2017 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() Commented Apr 11, 2017 at 9:24
  • The above line expands the drop down, but still i can't select the option for some reason. Commented Apr 11, 2017 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. Commented Apr 11, 2017 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()
answered Apr 28, 2017 at 15:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.