Thursday, August 29, 2024

xPath Cheat Sheet

XPath:

XPath stands for XML Path Language

XPath is a Selenium technique to navigate through a page's HTML structure. It enables testers to navigate any document's XML structure, which can be used on both HTML and XML documents.

XPath is a query language for selecting nodes from an XML document or for finding elements in HTML Webpage, used in most of the UI automation tools.

Types of XPATH:

  1. Absolute XPath – which starts from Root element (we use / as starting)
  2. Relative XPath – which starts from any element/ point in document (we use // as starting). Preferred as they are not complete XPath.

XPATH Attributes:

XPath can be written using attributes (eg: id, name, class)

Syntax: //tagName[@attributename]

eg: //input[@id]

XPath Operators:

There are many operators available to identify the element uniquely. Mainly used attributes are as below

=, !=, < , <=, >, >=

=,!= mostly used for text values

eg:

=: //input[text()=’test’]

!=: //input[text()!=’test’]

<, <=, >, >= are used for numerical values

eg:

<: //*[price<9]

: //*[price>65]

=: //*[price>=30]

<=: //*[price<=20]

XPath Conditions:

To identify a unique element sometimes we need to use multiple XPath’s and we need to concatenate multiple XPath’s using and/or/not

eg:

And: //*[@attribute1=’test’ and @attribute2 = ‘test3’]

Or: //*[@attribute1=’test’ or @attribute2 = ‘test3’]

Not: //*[@attribute1=’test’ or @attribute2 != ‘test3’]

Also, we can add multiple conditions to find the unique element.

XPath with indexes:

If none of the options are working to find the unique elements, we can use the indexes of the common group elements

eg: even though if we filtered 10 records after applying most possible conditions, then we need to use index as last option from 10 records

//input[@class=’yey’]/div[2]

XPath Functions:

Most common functions used to generate the XPath are text, contains, starts-with, normalize-space, last, position.

eg for text:

//input[@text=’test’]

eg for contains:

//div[contains(@class,’’)

//input[contains(text(),’test’]

eg for starts-with:

//*[starts-with(text(),’test’]

eg for normalize-space:

Normalize space will trim the spaces at start or end of the given value

//*[normalize-space(class) = ‘classname’]

//*[normalize-space(text())=’test’]

eg for last:

Using last we can get the las element of the xpath expression

(//input[@class=’yey’]/div)[last()]

eg for Position:

position in a context is not zero-based. The first node has a position of 1.

//div[@class='foo']/bar[position() = 1]

//div[@class='foo']/bar[position() >= 1]

//div[@class='foo']/bar[position() <= 1]

//div[@class='foo']/bar[position() > 10]

//div[@class='foo']/bar[position() < 10]

//div[@class='foo']/bar[position() != 1]

XPath Axes:

An axis represents the relation between the current node and to locate the node present on the tree

An XPath axe defines the node-set relative to the current (context) node. It is used to locate the node that is relative to the node on that tree.

Available axes:

‘Ancestor’ and ‘Ancestor or self’

Parent

Child

Sibling: preceding-sibling or following-sibling

‘Descendant’ and ‘Descendant or self’

Following

Preceding

Ancestor:

eg: //*[@class=’sample’]/ancestor::div – will retrieve all the nodes with div from the current node

Ancestor or self:

eg: //*[@class=’sample’]/ancestor-or-self::div – will retrieve all the nodes with div and the current node

Parent:

eg: //*[text()=’sample’]/parent::class -- will retrieve parent node from the current node

Child:

eg: //*[text()=’sample’]//child::class -- will retrieve child node from the current node

Sibling(preceding):

eg: //*[@id=’1’]/preceding-sibling::button[@name='settings'] -- will retrieve the preceding sibling of the current node

Sibling(following):

eg: //*[@id=’1’]/following-sibling::button[@name='settings'] -- will retrieve the following sibling of the current node

Descendant:

eg: //div[@class =’dou’]/descendant -- will retrieve all the following nodes from current node

Descendant or Self:

eg: //div[@class =’dou’]/descendant-or-self::a – will retrieve all the hyperlinks including current node

Following:

eg: //label[text()='value’]/following::input[1] -- will retrieve following node with input[1] from the current node

Preceding:

eg: //label[text()='Password']/preceding::input[1] -- will retrieve preceding node with input[1] from the current node

Shortcuts used in XPath:

Text()

we can use Dot(.) as shortcut to text() function

eg: //[text()=’shortcut’] ----> can be write as ----> //[.=’shortcut’]

Child

we can identify child nodes without using child key by simply writing node

eg: //[text()=’shortcut’]/Child::a ---> can be write as ----> //[text()=’shortcut’]/a

Parent

we can identify parent nodes without using parent key(using 2 Dots(..))

               eg: //*[text()=’shortcut’]/parent::div ---> can be write as ---> //*[text()=’shortcut’]/..

Descendant

We can use double forward slash(//) instead of descendant key

eg: //[text()=’shortcut’]/descendant::div ---> cab be write as ---> //[text()=’shortcut’]//div

To use below above() locator import below package import static org.openqa.selenium.support.locators.Relativelocator.*

above() relative locator

we can find the above of the current webelement

Syn: //withTagname(label).above(nameEditBox)

below() relative locator

we can find the belowof the current webelement

Syn: //withTagname(label).below(DateofBirth)

ToLeftOf() relative locator

we can find the webelement which is left of the current webelement

Syn: //withTagname(label).toleftOf(nameEditBox)

TorightOf() relative locator

we can find the webelement which is right of the current webelement

Syn: //withTagname(label).torightOf(nameEditBox)

 Refer: https://github.com/varunkapaganty/syntax-codesnippets/wiki/XPath-Learning

No comments: