Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watir / Ruby: How to get the text of the selected item in a drop down list?

Tags:

ruby

watir

Using Watir, how can I return the text of the currently selected item in a drop down list? It appears that getSelectedItems is deprecated.

like image 656
NinjaCat Avatar asked Jan 21 '23 13:01

NinjaCat


2 Answers

Use selected_options() instead.

selected_options () Description:

Gets all the selected items in the select list as an array. An empty array is returned if the select box has no selected item. Output:

Array containing the selected items of the select list.

Source

like image 64
Mahmoud Avatar answered May 01 '23 07:05

Mahmoud


The watir documentation also says you can do  

browser.select_list.selected_options.map(&:text)

but this returns the text in an array, I guess it would be useful to do it this way if you have multiple items selected in the list.

['selected text']
like image 40
Klassik_Peg Avatar answered May 01 '23 07:05

Klassik_Peg