I need to get a value of the selected HTML option:
<select name="DisplayCurrency" id="DisplayCurrency" >
<option value="GBP" >Pounds Sterling</option>
<option value="EUR" selected="selected" >Euros</option>
<option value="CHF" >Swiss Francs</option>
<option value="DKK" >Danish Krona</option>
<option value="CSK" >Czech Krowns</option>
<option value="HUF" >Hungarian Forints</option>
<option value="PLN" >Polish Zloty</option>
</option>
</select>
I tried this using Nokogiri, but it does not work:
page.search('//select[@id="DisplayCurrency"]/option[@selected=selected]').attr('value')
Here's some things about what Nokogiri returns when searching and more streamlined ways to git 'er done:
search
returns a nodeset, which is like an array. Be careful asking for an attribute of an node when you have a nodeset.Access a member of a nodeset by indexing:
This works but I consider it an unintended side effect:
This is the same thing but it fails with an exception:
Because you want the selected option inside a tag with an ID, I'd go after it using CSS selectors.
at_css
,at
and%
return a single node, simplifying the task.This works for me, even with the invalid markup: