Xpath to get the value of an element with Selected property - xpath

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

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

How to capture values from dropdown in JMeter

For code like below:
<div class="col-xs-4 col-sm-4 col-md-4 select-me show_me del_nxt" style="display: block;">
<select class="prime" name="primary" id="primary" onchange="newsecondary(this)">
<option value="none" id="1200">---Select main---</option>
<optgroup label="dummy1">
<option value="abc-2-1">abc</option>
<option value="xyz-2-1">xyz</option>
</optgroup>
<optgroup label="Dummy2">
<option value="abc1-2-1">abc1</option>
<option value="C1-2-1">C1</option>
<option value="D1-2-1">D1</option>
</optgroup>
</select>
<span class="Error"></span>
</div>
How to capture random value from dropdown list? thanks in advance.
Scenario is we have 4 drop downs with same type of html code as above.
Unless user selects any value from first drop down, another wont get enabled. this is how these 4 dropdowns are dependant on previous dropdown value .
Since content is html, the most maintainable way is to use CSS Selector Extractor based on this syntax:
Configuration would be the following:
You may use Regular Expression Extractor added to your request.
With the Regular Expression:
option value="([a-zA-Z0-9])+"
To ectract random value you need to set Match No to 0 as shown below
You may test your RegExp here regexr.com
Read more about Regular Expressions
If you want to select a random value and forget - go for HTML Links Parser
If you need the selected value anywhere else - you can extract it using i.e. XPath Extractor, it allows executing arbitrary XPath queries.
Get text of the selected option:
//select/optgroup/option/#selected/parent::*/text()
Get all options for optgroup with the label of dummy1
//select/optgroup[#label='dummy1']/option/#value
etc.

How to select particular option in a select list?

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"

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"

Extracting value from select element in HTML using XPath Query in JMeter

I want to extract the first value which has the property selected = "selected" using XPath extractor. But it doesn't seem to work for me.
The html which i'm extracting the value from is:
< select id="ddLocation" name="ddLocation" class="DDlocation" size="1" onchange="jsf.util.chain(this,event,'onLocationChange();,'mojarra.ab(this,event,\'valueChange\',\'#this\',0)')"> <br>
< option value="43" selected="selected">Pune</option> <br>
< option value="44">Agra< /option> <br>
< option value="45">Guntur< /option> <br>
< option value="46">Kochi< /option> <br>
< option value="73">Kothrud< /option> <br>
< option value="153">Ratnagiri< /option> <br>
< option value="156">Baner< /option>
My XPath query is:
//select[#id="ddLocation"]/option[1]/#value
Is it wrong?
Can anyone suggest me any better / right approach please?
Your xml is not in proper format
It has lot of spaces in-front of option and select is not closed at end.
<select id="ddLocation" name="ddLocation" class="DDlocation" size="1" onchange="jsf.util.chain(this,event,'onLocationChange();,'mojarra.ab(this,event,\'valueChange\',\'#this\',0)')">
<option value="43" selected="selected">Pune </option>
<option value="44">Agra</option>
<option value="45">Guntur</option>
<option value="46">Kochi</option>
<option value="73">Kothrud</option>
<option value="153">Ratnagiri</option>
<option value="156">Baner</option>
</select>
Finally, your XPATH works as expected.
//select[#id="ddLocation"]/option[1]/#value
It gives output as 43
EDIT:
If you use below XPATH, it gives result according to where attribute is selected=selected
//select[#id='ddLocation']/option[#selected='selected']/#value
I have not tested using JMeter but am checking XPATH on XMLSPY.
Since you are using XPath Extractor to parse HTML (not XML!..) response ensure that Use Tidy (tolerant parser) option is CHECKED (in XPath Extractor's control panel).
And use better refined xpath query from Siva's answer below.

Resources