Class name and value selection in xpath - xpath

I am new to x path and I am having a hard time resolving the following issue.
I am trying to access the below element <div class="MeCalendarMonthName">Aug 2013</div> using the below approach.
xpath=//div[#class='MeCalendarMonthName' and #value='Aug 2013']
and also I tried
(By.xpath("//div[contains(text(),'Aug 2013']").
But, I am failing with both approaches.
MeCalendarMonthName is a common class which is repeated many times in the tree, therefore I tried to use the combination of class name and value and tried to find only with the value. Can you please help me to find a suitable solution?

Try the below approach :
//div[#class='MeCalendarMonthName' and contains(.='Aug 2013')]
As per your comment you can use the below xpath:
//input[#value = 'Tue Aug 13 2013']
when the HTML
<input type="hidden" value="Tue Aug 13 2013">

xpath("//div[contains(.,'Aug 2013')]")

Related

Creating a valid XPath that checks two attributes

I am using FirePath to generate valid XPaths for Behat automation tests and frequently find myself with this issue:
I need to generate an XPath for the automation tests, i.e. to click on an element, but the path contains two parts that I need to check for to confirm it is the correct one
<div id="flash-success" class="alert-box with-icon info"> Operation note created. </div>
So in the above code I can use any of these valid XPaths that will result in one matching node:
//*[#id='flash-success']
//*[#class='alert-box with-icon info']
//*[contains(text(), 'Operation note created.')]
Ideally I want to confirm that the XPath checks two parts, the id/class AND the text, something like this:
//*[#class='alert-box with-icon info']//*[contains(text(), 'Operation note created.')]
But that is NOT a valid XPath. Can anyone shed any light here, I have tried reading up on W3 and questions on here but have yet to find a solution
If you want to find an element were all 3 of your conditions must be true you can write:
//*[#id='flash-success'][#class='alert-box with-icon info'][contains(text(), 'Operation note created.')]
or
//*[#id='flash-success' and #class='alert-box with-icon info' and contains(text(), 'Operation note created.')]
If you want to find an element were ANY of your condtions are true you would write:
//*[#id='flash-success' or #class='alert-box with-icon info' or contains(text(), 'Operation note created.')]
As a sidenote, usually when checking against a class attribute in html you would do contains(#class,'alert-box') since there usually are mulitple classes that are space separated, which are often generated and do not have to be in any order.

Extracting content between two tags with XPath

I've just started working with XPath recently and run into a problem. Here is the code I want to extract from:
<h3>Some Company</h3>
Mainstreet 1234
<br>
98776, Country
<br>
How would I extract the content between the closing h3 and br tag?
Try //h3/following-sibling::text()[following::br]
This could work h3/following-sibling::node()[not(preceding-sibling::br) and not(self::br)] (returns "Mainstreet 1234" for me).
But I'm affraid your real xml and real needs are more complicated than provided sample so it is possible you will need to further adjust it to fit you requirements.
If your code was in the block below:
<par>
<h3>Some Company</h3>
Mainstreet 1234
<br>
98776, Country
</br>
</par>
You will need to tell XPath to give you the text inside every par node that is after an h3 node and before a br node.
In XPath terms this translates to:
//par/text()[preceding::*[name()='h3'] and following::*[name()='br']]
The above would search everywhere in the document for a par node. You can get more specific about the content of the h3 and/or br nodes as well:
//par/text()[preceding::*[name()='h3' and text()='Some Company'] and following::*[name()='br']]
Please let me know if the above does not resolve your problem.

how to find XPath for complicated tag without id or name

I have to find out XPath for code :
<td>
<input type="button" onclick="redirectToUserList(5);" class="btnManage" value="Manage Users" style="background-color: transparent;">
Using firebug the XPath is :
/html/body/div/div/div[4]/table/tbody/tr/td/table/tbody/tr[2]/td/div/table/tbody/tr/td
/table/tbody/tr[5]/td/div/div/form/table/tbody/tr[2]/td[4]/input
But how can I have a shorter XPath? For example, I wish the following could run:
//input[#value='Manage Users']
Please advise how to find shorter XPath using standard syntax?
It is not mandatory to use id or name. You can use any attribute of that element.
Following are different xpaths for given locators
1. "//input[#value='Manage Users']"
2. "//input[contains(#onclick,'redirectToUserList')]"
3. "//input[#type='button' and #class='btnManage']"
You do not need to search for id or name, you can search for any attribute.
For example:
//input[#value="Manage Users"]
which sounds unique
As an addon to above given answers any specific attribute like id or name is never necessary.
We mostly take id and value to identify an element uniquely because other attributes may specify some other locators as well.
If any attribute is not creating confusion with some other locator you can choose it without any worry.
Lastly if you are not so good in writing xpath and also dont like large xpaths given by firebug just install an addon of firebug called as Firepath it will give you the shortest feasible xpath.
You can also go with other addons like Xpath Checker etc.

Use XPath to select the element with a certain token in the value

I have the following XML:
<ZMARA SEGMENT="1">
<MATERIAL>000000000030001004</MATERIAL>
<PRODUCT_GROUP>14000IAA</PRODUCT_GROUP>
<PRODUCT_GROUP_DESC>HER 30 AR NEW Size</PRODUCT_GROUP_DESC>
<CLASS_CODE>I046</CLASS_CODE>
<CLASS_CODE_DESC>Heritage 30</CLASS_CODE_DESC>
<CHARACTERISTICS_01>,001,PLANNING_ALERT_PERCENTAGE, 50.000,PLANNI</CHARACTERISTICS_01>
<CHARACTERISTICS_02>X,001,COLOR_ATTRIBUTE,Weathered Wood,WEWD,Col</CHARACTERISTICS_02>
<CHARACTERISTICS_03>,001,ARMA_UOM,SALES SQUARE,SSQ,ARMA UNIT OF M</CHARACTERISTICS_03>
<CHARACTERISTICS_04>,001,ARMA_A_CATEGORY,05-Below 260 Lam/Multi-l</CHARACTERISTICS_04>
</ZMARA>
Using XPath I need to select the CHARACTERISTICS_XX element whose value contains the COLOR_ATTRIBUTE token. It will not always be characteristics_02. Thanks for the help. I am a total noob at XPath.
This looks like its taken from a sap idoc, you can probably be lucky that the fieldnamed are not 6 character long abbreviations :)
The answer given by spinon is correct, however if there could be another element that contains the text 'COLOR_ATTRIBUTE', this would give a more specific match:
/ZMARA/*[starts-with(local-name(.), 'CHARACTERISTICS_')][contains(.,'COLOR_ATTRIBUTE')]
Another suggestion is to avoid the '//' expression if you know where the ZMARA element can occur, in the expression above ZMARA would only be searched as a root element which would be more performant.
This should work:
//ZMARA/*[contains(.,'COLOR_ATTRIBUTE')]

XPath Find full HTML element ID from partial ID

I am looking to write an XPath query to return the full element ID from a partial ID that I have constructed. Does anyone know how I could do this? From the following HTML (I have cut this down to remove work specific content) I am looking to extract f41_txtResponse from putting f41_txt into my query.
<input id="f41_txtResponse" class="GTTextField BGLQSTextField2 txtResponse" value="asdasdadfgasdfg" name="f41_txtResponse" title="" tabindex="21"/>
Cheers
You can use contains to select the element:
//*[contains(#id, 'f41_txt')]
Thanks to Thomas Jung I have been able to figure this out. If I use:
//*[contains(./#id, 'f41_txt')]/#id
This will return just the ID I am looking for.
I suggest to not use numbers from Id , when you are composing xpath's using partial id. Those number reprezent DINAMIC elements. And dinamic elements change over the next deploys / releases in the System Under Test.The pourpose is to UNIQUE identify elements.
Using this may be a better option or something like this, yo got the idea:
//input[contains(#id, '_txtResponse')]/#id
It worked for me like below
//*[contains(./#id, 'f41_txt')]

Resources