Is there a way to use Watir to select an item from a dro pdown list by item number? That is, when using SelectList
, does option()
take anything else besides value
and text
?
I'm trying to run a process on a webpage where I select the first item on a drop down list, do an operation, go to the second item, do the operation again, etc. And this drop down list has over 700 options!
Here's what the HTML looks like:
<SELECT NAME="sl" SIZE="1">
<option value=""> </OPTION>
<option value="abq">Abaza</option>
<option value="abk">Abkhazian</option>
...
<option value="zun">Zuni</option>
</SELECT>
The SelectList
documentation suggests something like:
b.select_list(:name => 'sl').select_value("abq")
The question is then how do I move to the next value? And the next 700? Is there a way to select by item number? Or extract the values into an array and then cycle through them?
Thanks!
When you want to select an option element by anything other than text or value, you can locate and select the element directly. For example, equivalent to using the select_value
method is:
b.select_list(:name => 'sl').option(value: 'abq').click
You can use the usual locators. To select the second option, you can use the :index
locator (note that it is 0-based):
b.select_list(:name => 'sl').option(index: 1).click
Note that given you want to iterate through each option, it might make more sense to use one of the Enumerable methods to do the loop. This can eliminate the need to worry about the index. However, may not work depending on what the other actions you are planning to take.
b.select_list(:name => 'sl').options.each do |option|
option.click
# Do other actions
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.