XPath in Selenium WebDriver

XPath in Selenium WebDriver

Locators

In Selenium automation, Locators are used to locating the elements on webpages but very often it has been observed that either multiple matching elements are located or not even any element is located. So, in order to accurately locate the elements on the webpage, the XPath locator is used.

There are different types of locator in selenium which we can use and find the web elements.

  1. Id
  2. Name
  3. Class Name
  4. Tag Name
  5. Link text and Partial Link Text
  6. CSS Selector
  7. XPath

XPath is defined as an XML path. It helps in locating any element on the webpage using an XML path expression. The fundamental syntax of XPath which is explained below with screenshot.

XPathXPath

Types of X-path

1) Absolute XPath

2) Relative XPath

Absolute XPath

  • Absolute XPath begins with a single forward slash (/), which means you can select the element from the root node.
  • It uses a complete path from the Root Element to the desired element.
  • The advantage of using absolute is, the identification of the element becomes very fast.

The disadvantage is, if anything goes wrong like some other tag added or removed in between, the path will no longer work.

XPathRelative XPath:

  • Relative XPath starts from the node of your choice. It is not necessary to start from the root node.
  • It starts with the double forward slash (//) which indicates it can search the element anywhere on the webpage.
  • If the same path consists of multiple elements, it will select the first element that is identified.
  • The advantage is, no need to write long XPath.

XPath

Handling dynamic elements in Selenium Using XPath

1.    Basic XPath:

  • XPath expression select nodes based on attributes like ID, Name, Classname, etc.

XPath

2.    Contains ():

  • With the dynamically changing values, contains () is used prominently.
  • The contain feature is used to locate the elements with partial text.

XPath3.    Using OR & AND

  • In OR expression, from two conditions, any one condition should be true to find the element.
  • In AND expression, from two conditions, both conditions should be true to find the element.

XPathXPath4.   Starts-with ()

  • This is used when the initial partial attribute value of initial text associated with the web element is fixed. Which consists of both static (initial) and dynamic (trailing) values.

XPath5.   Text ()

  • The elements with the exact text match can be found as mentioned below. The Text () is a case sensitive. In our example, we find the element with the text “Sign in”.

XPath6.   position ()

  • It is used to locate the element node at the specified position of the specified node type.

Other e.g.

  • //center/input[position( )>1] :

2nd no. (I’m Feeling Lucky) input button highlight and 1 matching node.

  • //center/input[position( )>0] :

1st no. (Google search) and 2nd no. (I’m Feeling Lucky) input button highlight and 2 matching nodes.

  • //center/input[position()>0 and position()<3] :

2 matching node and both input button highlighted.

XPath7.   Single Attribute

  • In order to exclude HTML Tag, then you need to use ‘*’
  • In order to include HTML Tag, then do not need to use ‘*’

Below both images verify matching node for same path and text.

  • In 1st Example, when we use HTML tag(p) then first check HTML tag(p) after going into HTML tag’s attribute (we use text () method) and match with attribute’s value (Sign in) and select “Sign in” text.
  • In 2nd Example, we use * sign and then check in “Sign in” text in whole HTML Document.

XPathXPath

TIPS

Firebug and fire path plugin are not supported in the later versions of Firefox 56. So you can find XPath in Google Chrome.

  1. Right-click on any element and click on “Inspect element”.
  2. Press “Ctrl+F” key and type relative XPath and find the element.
The following two tabs change content below.
Shakuntala Choudhary

Shakuntala Choudhary

QC Automation Engineer at Dev Information Technology Ltd.
I'm a QC Automation Engineer of Testing Department, at Dev Information Technology Limited. I look upon the projects related to Automation & Performance Testing. I having knowledge of the Automation Testing.
Shakuntala Choudhary

Latest posts by Shakuntala Choudhary (see all)

One thought on “XPath in Selenium WebDriver

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>