Xpath in Selenium

What is XPath :

XPath (XML path) is the language for nodes in an XML document. Since many browsers support XHTML, we can use XPath to find items on web pages.

XPath helps in finding direction of element through nodes and its attributes. in HTML pages.

 

Syntax of XPath:

 

Using tag and attributes

It is possible to locate a specific web element with its HTML tag, its attributes such as Id, name, class, title, value, href, src etc., and their corresponding values.

Syntax: “//tagName[@attribute=’value’]”;

Identifies the element pointed by the XPath. ‘//’ identifies the specified node and ‘@’ symbol is used to select the specified attribute which matches the given value.

  • //input[@name=’FirstName’]
  • //input[@id=’abc’]

 

Xpath with Index:

By providing the index position in the square brackets, we could move to the nth element.

  • //label[2]

 

Xpath With Parent Reference:

 

 

Xpath with Group Index:

 

 

text() function in Xpath:

text()  is used to locate an element based on the text available on a web element.

//*[text()=’exact text with exact case’]

 

Dependent and Independent Xpath:

 

 

contains() function in Xpath:

Contains method is used when we know about the partial attribute value or partial text associated with the web element.

 

By Text

  • //a[contains(text(), ‘oog’)]
  • //a[contains(text(), ‘og’)]
  • //a[contains(text(), ‘Goo’)]

By Attribute

  •  //a[contains(@id,’login’)]
  • //a[contains(@class,’highlight’)]

 

Normalize Space in Xpath:

 

 

starts-with function xpath:

starts-with() method is used when we know about the starting partial attribute value or starting partial text of  the web element.

User can also use this method to locate web elements those are consist of both the static(initial) and dynamic(trailing) values.

 

  • //a[starts-with(@class,’high’)]
  • //a[starts-with(text(),’log’)]

 

ends-with function xpath:

The ends-with function is part of XPath 2.0 but browsers generally only support 1.0.

Ends-with function matches the elements properties ending value

syntax ://xpath[ends-with(@attribute,'ending value')]

 

Last() function in Xpath:

Finds the last element with given xpath out of all matches, it is also an index but the last one.

//input[@type=’button’][last()]

 

Position function in Xpath:

Finds the element with given position when we have more than one match.

//input[@type=’button’][position()=2]

 

 

CaSe in-sensitive Xpath in selenium webdriver:

 

 

 

Attribute value’s Length xpath in Slenium:

 

 

Relational value Xpath in selenium webdriver:

 

 

Axes in Xpath in selenium webdriver:

 

 

 

 

 

 

Leave a Reply

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