How to select particular option in a select list? - ruby

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:
It gets a list of matching options based on text, label and value and
Then for each matching option Watir-Classic selects it.
For your specific example, this means that
2 options match the input "2":
<option value="2">1</option> matches since its value is "2"
<option value="3">2</option> matches since its text is "2"
Watir-Classic is selecting each of these options, in this specific order, which is why you see the dropdown switch to "1" and then "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"

Related

How to set default value for input inside select option

Hello Guys I Have select and many options and i have input inside on of these options and i want to set default value for it. My Code is bellow
<label for="cars">Choose a car:</label>
<select name="cars" id="cars" class='select2'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
i want to set default value for input id = 'searchfor'.
i tried this code bellow but it doesn't work
<select name="cars" id="cars">
<input id='searchfor' type='search' value='car1' placeholder='search for your car'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
when i open the select i don't see the value for this input
You cannot put an text input element inside a select element
Using MDN as a reference, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select the only permitted content for a select element is;
Zero or more <option> or <optgroup> elements.
So it is not valid html for a select input element to itself contain an input element
See also for info;
Put input inside select

inspection of code shows code that should not be there or be there as it is

I'm working with an application written in dHTML and running on a Windows 10 laptop using the Apache server. I have some lines of simple code that generate a 2 SELECT statements as follows.
print " Choose an effort: <SELECT NAME='echoice' CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER=\"setStatus2('Only efforts with measures your authorized to work with are shown.');\" ONMOUSEOUT=\"setStatus2(' ');\" ONCHANGE=\"Loadem('choice');\">\n";
foreach $iline (#ilines) {
print "<OPTION VALUE='$iline'>$iline</OPTION>\n";
}
print "</SELECT>\n";
print "Choose $what: <SELECT NAME='choice' CLASS='slcts' SIZE='16' STYLE='width:180px'>\n";
if ($cmfunction eq 'MeasureDescriptions' && $action eq 'U') {
print "<OPTION SELECTED VALUE='new'>new</OPTION>\n";
$imo = 1;
}
foreach $mline (#mlines) {
$thestr = ($i == 0 && !$imo) ? 'SELECTED' : '' ; # Select 1st measure if not a single model.
print "<OPTION $thestr VALUE='$mline'>$mline</OPTION>\n";
$i++;
}
print "</SELECT>\n";
I don't seem to be able to change the contents of this field as long as the first select is there. First I put a new unique character in the 2nd SELECT text each test to tell me if I'm executing the module version I think I am. I always see the unique character.
I have tried the following.
comment out the inner 2 print statements,
change >new< to >new1<,
assign 'dog' to the 0 position of the mlines array, and
add a print after the Option loop to add a 'dog' option.
In all four cases, the choice SELECT had the same options as it had before any of the tests (1-4) were implemented. But if I remove the first select,
I can then modify the options of the 2nd select. The debugger image of the above code follows with both selects.
Choose an effort: <SELECT NAME='echoice' CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER="setStatus2('Only efforts with measures your authorized to work with are shown.');" ONMOUSEOUT="setStatus2(' ');" ONCHANGE="Loadem('choice');">
<OPTION SELECTED VALUE='all'>all</OPTION>
<OPTION VALUE='Health'>Health</OPTION>
<OPTION VALUE='HealthCare'>HealthCare</OPTION>
</SELECT>
Choose a measure !: <SELECT NAME='choice' multiple CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER="setStatus2('Only measures your authorized to work with are shown. Select a measure for a formal definition.');" ONMOUSEOUT="setStatus2(' ');" ONCLICK='doFRef(event,this,this.form)'>
<OPTION SELECTED VALUE='new'>new</OPTION>
<OPTION VALUE='HEComp90'>HEComp90</OPTION>
<OPTION VALUE='HECompAtBirth'>HECompAtBirth</OPTION>
<OPTION VALUE='healthy'>healthy</OPTION>
<OPTION VALUE='infant'>infant</OPTION>
<OPTION VALUE='maternal'>maternal</OPTION>
<OPTION VALUE='stillbirths'>stillbirths</OPTION>
</SELECT>
The debugger code with only one select follows. I changed new to new1 and added the line and line2 options. But if I put the first select back in,
I can't modify the 2nd select and it contains what is shown in the 1st debugger image.
<OPTION SELECTED VALUE='new'>new1</OPTION>
<OPTION VALUE='line'>line</OPTION>
<OPTION VALUE='HEComp90'>HEComp90</OPTION>
<OPTION VALUE='HECompAtBirth'>HECompAtBirth</OPTION>
<OPTION VALUE='healthy'>healthy</OPTION>
<OPTION VALUE='infant'>infant</OPTION>
<OPTION VALUE='maternal'>maternal</OPTION>
<OPTION VALUE='stillbirths'>stillbirths</OPTION>
<OPTION VALUE='line2'>line2</OPTION>
</SELECT>
I don't understand this. Any help will be appreciated. It must be me.
Thanks,
craigt
It was me. There is an ONLOAD process that executed another process that preps
the content of that field that was not written correctly. I've corrected that process and all works as expected now. This question is closed. And I apologize for my oversight and thank everyone that took a look.

How to get an index of specific text in dropdown using vbscript code(.vbs)?

I am working on vbscript. I am writing script for selecting value from dropdown.
In my scenario, I want to select a value from dropdown using the text which resides in option tag i.e <option>value<option>
Request you to provide code for selecting value in dropdown using value not by index of value.
Thank you in advance. Please reffer below html code for reference.
<select id="mydropdown" onchange="getServiceDetails();" name="mydropdown">
value="[]"
<option value="-1">Select value</option>
value="[]"
<option value="0920102049">value 1</option>
value="[]"
<option value="0060217015">value 2</option>
</select>
This will do it.
Sub SelectOptionByInnerHTML(selectID, text)
Dim list, opt
Set list = document.getElementById(selectID)
For Each opt In list
If opt.innerHTML = text Then
opt.selected = true
Exit Sub
End If
Next
End Sub
Usage:
SelectOptionByInnerHTML "mydropdown", "value 2"

Xpath to get the value of an element with Selected property

For the following XMl file,
<select id="pet" title="Pet" class="x8" onchange="" name="pet">
<option></option>
<option selected="" value="abc">Dog</option>
<option value="def">Cat</option>
<option value="ghi">Rabbit</option>
</select>
What is the Xpath to be able to get the value of the option with "Selected" property? (I need to get "abc")
One possible way to get value attribute of option element having selected attribute :
/select/option[#selected]/#value
That would take all options
/select/option
That would take 2-nd option and read value attribute from it.
/select/option[2]/#value

xpath for selecting multiple elements from drop down

I am trying to extract some information from an html page. Consider the drop down select list below:
<select name="ctl00$MainContent$ddlColor" onchange="chageColor(this);setTimeout('__doPostBack(\'ctl00$MainContent$ddlColor\',\'\')', 0)" id="ctl00_MainContent_ddlColor" class="input" style="width:175px;">
<option selected="selected" value="">Color</option>
<option value="00114743-03|large|0|03">CHARCOAL</option>
<option value="00114743-04|large|2|04">BLACK</option>
</select>
It has 3 values, "Color", "CHARCOAL" and "BLACK".
Now if I view the source and copy XPATH of "CHARCOAL" using google chrome, I get
//*[#id="ctl00_MainContent_ddlColor"]/option[2]
However, I want to extract the information as "CHARCOAL" and "BLACK". I want this to be applied on multiple pages where the drop down list might contain more or less number of elements. However, I always want to skip the first element, which will be "Color". How to do this ?
Here you are ..
//select/option[not(contains(., 'Color'))]/text()
You can skip the first option by it's position by this
//select/option[position() > 1]/text()
I hope this could help

Resources