0

I am having an issue extracting all the options in a drop down and then iterating through values to get selected value. Ruby code Below:

select = @@driver.find_element(:id, 'dropdown_7')
            all_options = select.find_elements(:tag_name, 'option')
            all_options.each do |i|
              puts 'Value is: ' + i.attribute('Andorra')
              i.click

HTML code:

<select id="dropdown_7" name="dropdown_7" class="  piereg_validate[required]"><option value="Afghanistan">Afghanistan</option></select>

Error Message: `+': no implicit conversion of nil into String (TypeError)

Not sure what this means apart from + = nil and no conversion of string?

Speedychuck
  • 400
  • 9
  • 29

1 Answers1

1

The error is thrown because i.attribute('Andorra') returned nil which ruby failed to convert to a string. Here are a few examples that should get you what you want:

# print the name attribute
puts 'Name is: %s' % i.attribute('name')

# print the value attribute
puts 'Value is: %s' % i.attribute('value')

# print the text content
puts 'Text is: %s' % i.text
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Ok thanks that worked, it is now iterating through the values, however when it finds Andorra the selected attribute it doesn't select it just keeps iterating the values in the dropdown, when it needs to select Andorra once it finds it in the options list. By way still new to ruby, how come % works? I thought this was only used in maths? – Speedychuck Apr 08 '16 at 15:24
  • The percent is used to format the string. As for your issue, there is not enough information to provide a better solution. Have a look at this example, it might help you: http://stackoverflow.com/questions/4672658/how-do-i-set-an-option-as-selected-using-selenium-webdriver-selenium-2-0-clien – Florent B. Apr 08 '16 at 15:44