I'm trying to select option 2, but first it is selecting 2 then changing to 1.
This is the HTML of the select list:
<select id="IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id">
<option style="background-color: rgb(252, 218, 175);" value="-1">Select</option>
<option value="1">0</option>
<option value="2">1</option>
<option value="3">2</option>
<option value="4">3</option>
<option value="5">4</option>
<option value="1000000">5</option>
<option value="1000001">more than 5</option>
</select>
This is the Watir code for selecting 2:
b.select_list(:id,"IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id").select "2"
The problem is that you are using Watir-Classic's select
method with a parameter that matches both an option's text as well as another option's value.
If you take a look at the code for the Watir::SelectList#select
method :
def select(item)
matching_options = []
perform_action do
matching_options = matching_items_in_select_list(:text, item) +
matching_items_in_select_list(:label, item) +
matching_items_in_select_list(:value, item)
raise NoValueFoundException, "No option with :text, :label or :value of #{item.inspect} in this select element" if matching_options.empty?
matching_options.each(&:select)
end
first_present_option_value matching_options, :text
end
You can see that:
For your specific example, this means that
<option value="2">1</option>
matches since its value
is "2" <option value="3">2</option>
matches since its text
is "2" Given the way the method is written and how your select list is written, you cannot use the select
method. While the best choice is to move to Watir (previously Watir-Webdriver), there are workarounds in Watir-Classic.
Remove the ambiguity by specifically selecting an option based on only the value
attribute:
id = "IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id"
b.select_list(:id, id).select_value "3"
#=> will select <option value="3">2</option>
If you want to stick to selecting by text, directly locate/select the option element:
id = "IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id"
b.select_list(:id, id).option(:text, "2").select
#=> will select <option value="3">2</option>
For Watir, you can use index based options where you need to worry about index only. Such as for:
<option value="2">1</option>
We can use,
browser.select_list(id: 'IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id').options[2].select
As it is the 3rd index of given select_list and it follows zero based index ordering. Similarly for,
<option value="3">2</option>
We can use
browser.select_list(id: 'IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id').options[3].select
As it is the 4th index of given select_list.
#select
matches the text. If you want to select by value you need to use #select_value
:
b.select_list(id: "IDITForm@additionalVehicleDrivers|1@youngestDriverExperienceVO@id").select_value "2"
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.