Let us learn about Xpath and most common syntax's...


Xpath: Generally Xpaths are of two types.

1.Absolute path
2.Relative path

Absolute Xpath:

                  Absolute XPath starts with the root node or a forward slash (/) and the advantage of using absolute is, it identifies the element very fast and the disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.

Example for Absolute Xpath: /html/body/div[2]/div/div/footer/section[3]/div/ul/li[3]/a


Relative Xpath:

                  Relative Xpath starts from the current element which you needs to be located. Its starts with (//) and the advantage of using relative Xpath is, you don't need to mention the long xpath, you can start from the middle or in between.

Example for Relative Xpath:
//*[@id=’social-media’]/ul/li[3]/a

Following are the few examples on xpath generation:

Example 1:

<ul id="country">
  <li>India</li>
  <li>US</li>
  <li>UK</li>
  <li>China</li>
</ul>

From the example 1,list li tags values are dynamic.if you refresh web Page the values in the li tags may vary.

  1. How to generate an xpath to get the Last li tag value if the values in li tags are dynamic:
    • Xpath to get Last li tag value: //ul[@id='country']/li[last()]
  2. How to generate an xpath to get the last but one li tag value if the values in li tags are dynamic:
    • Xpath to get Last but one li tag value: //ul[@id='country']/li[last()-1]
  3. How to generate an xpath to get the last but second li tag value if the values in li tags are dynamic:
    • Xpath to get Last but one li tag value: //ul[@id='country']/li[last()-2]

Example 2:

Locating Elements by Attribute:
Form the example 2,possible ways to generate xpath based on attributes
Solution: 
Xpath based on “id” attribute:
  • xpath=//input[@id='userName']
Xpath based on “name” attribute:
  • xpath=//input[@name='login_userName']
Xpath based on “class” attribute:
  • xpath=//input[@class ='user_class']

Example 3:
<button id="submit_800"/>
<button id="submit_508"/>

From the example 3, In my webpage submit button is there and its id is dynamic, every time id attribute value is appending some numeric number at the end.
Solution: For this type of requirements,we can generate an xpath like this:

Xpath=//button[starts-with(@id,'submit_')]


Example 4:
<button id="800_submit"/>
<button id="508_submit"/>


From the example 4, In my webpage submit button is there and its id is dynamic, every time id attribute value is appending some numeric number appending before.

Solution: For this type of requirements,we can generate an xpath like this:

Xpath=//button[ends-with(@id,'_submit')]

Example 5:
Xpath based on text():From the example 5,suppose user needs to generate an xpath based on the text value

Solution : F
or this type of requirements,we can generate an xpath like this:
                 Xpath=//h2[text()='Welcome to Selenium Automation']

Xpath based on contains:
From the example 5, need to generate an xpath based on the  substring of text value
Solution : For this type of requirements,we can generate an xpath like this:
                 Xpath=//h2[contains(text(),'Selenium Automation')]

Example 6:
<ul id="country">
  <li>India</li>
  <li>US</li>
  <li>UK</li>
  <li>China</li>
</ul>

How to get the li node values between “US” and “China”:
Solution: By generating xpath by using preceding and following sibling, we can get the “UK” value between “US” and “China”


Solution : Xpath=//ul[@id=‘country’]/li[preceding-sibling::li[contains(text(),‘US’)] and following-sibling::li[contains(text(),‘China’)]
Categories: