0

I know this question sounds like a repeat but I have tried solutions I have found on here and none have worked for me thus far.

Implementing solutuons from the following questions:

https://stackoverflow.com/questions/7489917/using-selenium-2-and-firefox-how-do-you-select-a-dropdown-selection Selenium WebDriver and DropDown Boxes

Reference for methods in Ruby: http://selenium.googlecode.com/svn/trunk/docs/api/rb/index.html#selenium-webdriver

I am using selenium with cucumber to test a website.I am working in IE 8.

chan_text = @driver.find_element(:css,'select#check-list option').text
select_list = @driver.find_element(:css,'select#check-list')
dropdown = Selenium::WebDriver::Support::Select.new(select_list)
dropdown.select_by(:text, chan_text)

The source looks like the following:

<select id="check_list" multiple="">
<option selected="" value="81" label="MILK">MILK</option>
<option value="82" label="CHEESE">CHEESE</option>
<option value="83" label="DOUGHNUTS">DOUGHNUTS</option>
</select>

When an option is selected the page should refresh. This is not occurring at all, the menu just remains open. Any help appreciated, please request more info if needed. I will delete this question if it really is a repeat.

2
  • Need the webpage url,to see the page source html. Commented Jun 26, 2013 at 16:01
  • Sorry I am not able to share the webpage. If an answer is not possible without it I will delete the question and find another way to ask. Commented Jun 26, 2013 at 16:06

2 Answers 2

1

What do you get if you do this?

chan_text = @driver.find_element(:css,'select#check-list option').text
puts chan_text

my bet is, not what you are looking for.

But if you do this -

chan_text = @driver.find_element(:css,'select#check-list option:nth-child(2)').text
puts chan_text #"CHEESE" is what will be returned

The reason being your selector for the find_element (for chan_text), returns all the option values, where as I assume you only want one. So appending :nth-child(n) in your css selector, will help you select the option you are particularly interested in.

Sign up to request clarification or add additional context in comments.

7 Comments

This was not the exact solution but it did guide me to it! By selecting an item with Nth child I noticed the item un-highlight. Which made me realize ah the item is selected already! So instead of trying to access the particular option I just did a send_keys :return which worked. I realize not being able to see the source makes it difficult for people to help. I think I will delete this question as the solution could not be specifically found since so much of my problem was not known.
Actually I think I will mark this as the answer. If this was exactly my problem then your solution would have worked.
Actually, the answer is incorrect in ascertaining that all of the option values would be returned. He is using find_element (not find_elements) which will only return a single element, the first element found. My guess is that like he indicated, when he got the text from the option element, the focus was given to the select control, but the event listener to update the page was never fired. I would encourage @megaxelize to work with the devs to determine which exact event is being listened for and then trigger it appropriately.
@bgoad, the answer is not incorrect. Based on how Megaxelize's question, has this answer been provided. If she wanted to COLLECT all available options and then select a particular option from that obtained list/array, then using find_elements would help doing that. Then iterate over that list/array of elements can find the text fro each of them and store in another array. Another solution would be to write javascript to do all that in one line/step.
@bgoad, my answer provides a way to select A option and not ALL options.
|
1

Here's a better option that I found:

#Select the dropdown button 
dropdown_list = driver.find_element(:id, 'check_list')

#Get all the options from the dropdown
options = dropdown_list.find_elements(tag_name: 'option')

#Find the dropdown value by text
options.each { |option| option.click if option.text == 'CHEESE' }

Comments

Your Answer

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.