Suppose I have following code:
<div class="Class">
<h3>First Title H3</h3>
First Description <br />
Choose Option:
<select id="Id" name="Name">
<option value="Value1">Option1 $Price1</option>
<option value="Value2">Option2 $Price2</option>
<option value="Value3">Option3 $Price3</option>
</select>
<h3>Second Title H3</h3>
Second Description Link
</div>
What's the Xpath to print "First Description" only?
What's the XPath to print "$Price1" only (without/exclude "Option1")?
Thanks for your help.
I'm not sure what is your desired output, but here is what should help you.
following-sibling::text() would help you to check the First Description:
//div/h3[contains(following-sibling::text(), "First Description")]
For the first option of the select tag, I would rely on the select's id attribute and option's value attribute:
//select[#id="Id"]/option[#value="Value1"]/text()
Solved based on this answers: using XPath: how to exclude text in nested elements and XPath: select text node and Get second element text with XPath?
String value = driver.findElement(By.xpath("//*[#value ='Value1']").getText();
String strarr[] = value.split(" ");
System.out.println("Price1 :"+ strarr[1]);
I think this will work, we are finding the text and splitting with space.
Related
How to access "Testing Field 1" from Label without using contains or adding span tag in Xpath
<label id="138:" for="136:" class="rcmFormFieldLabel">
<span class="requiredField" aria-hidden="true" role="presentation">*</span>
Testing Field 1
</label>
my xpath
//label[contains(text(),'Testing Field 1')]/ancestor::div[1]//select
but my xpath have contains which i dont want
You can use these XPath-1.0 expressions:
This
//label/*/following-sibling::text()[normalize-space()]
Or this
//label/text()[normalize-space()]
Result in both cases is
Testing Field 1
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.
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
i have a text box in my web application,Where i need to give input. I am trying to find the xpath of the text box. the following error is thrown.
Unable to locate element: {"method":"xpath","selector":"
HTML code:
<div class="input">
<input id="firstName" class="long" type="text" maxlength="50" value="" name="firstName
I want the xpath for firstName textbox.
//input[#type='text']
And this for generally targeting a text input (what I was after)
Try this one:
//input[#id='firstName']
Explanation:
// search on all levels
input for element nodes with the name of "input"
[#id='firstName'] having an attribute (#) with the name of "id" and a value of "firstName"
at least 3 simple ways to get this:
1)Driver.FindElement(By.XPath("//input[#id='firstName']"));
2)Driver.FindElement(By.Id("firstName"));
3)Driver.FindElement(By.CssSelector("#firstName"));
//*[text()[contains(.,'firstName')]]
finding by text would always work.
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.