Skip to main content

XPATH Basics....


XPATH Basics...


  • XPath is a text syntax for locating parts of HTML document
  • XPath contains a library of standard functions which helps to locate the WebElement
  • XPath is a W3C recommendation

XPath uses path expressions to select WebElement or sets of WebElements in Web page. These expressions look very much like the expressions you see when you work with a traditional computer file system. like c:\MyFolder\another_folder\myFile.txt

XPATH terminologies

Nodes

While using Selenium WebDriver, we come across following three types of nodes
  1. element
  2. attribute
  3. text
Look at below HTML code sample

  • here div, a are element nodes (element is the basically type of WebElement).
  • Attribute nodes for div element is class and its value is myLink
  • "a" element has text nodes as Link to Google, Link to Yahoo, Link to FaceBook

Relationship of Nodes

There are various relations available in XPATH but as a selenium user we must have knowledge of below relations

Children

Element nodes may have zero, one or more children.
above example Google Link, Yahoo Link, and FaceBook Link are Children of div tag which having class myLinks

Descendants

A node's children, children's children, etc. called as Descendants.
Similarly Google Link, Yahoo Link, and FaceBook Link are Descendants of div tag which having class header

Selecting Elements

XPath uses path expressions to select nodes in an Web document. The node is selected by following a path or steps. The most useful path expressions are listed below:
ExpressionDescription
nodenameSelects all nodes with the name "nodename" Example div, a, input, button etc.
/Selects child from the current node
//Selects Descendants from the current element that match the selection no matter where they are
. Selects the current node
..Selects the parent of the current node
@ Selects attributes
In the table below we have listed some path expressions and the result of the expressions:
Path ExpressionResult
/divSelects root level div from the whole page in above example it is div having header class
/div/divSelects all div which are child of root div in above example it is div having myLinks class
//divSelects all div from the page no matter where they are

Predicates

Predicates are used to find a specific node or a node that contains a specific value.
Predicates are always embedded in square brackets.

 
  Page Title
 
 
  
  

This is a Home Page

This is a paragraph 1. read more

This is a paragraph 2. read more

This is a paragraph 3. read more

This is a paragraph 4. read more

This is a paragraph 1. and its detailed explanation

This is a paragraph 2. and its detailed explanation

This is a paragraph 3. and its detailed explanation

This is a paragraph 4. and its detailed explanation

In the table below we have listed some path expressions with predicates and the result of the expressions for above example

Path ExpressionResult
/div[1]Selects the first div element from the root that is div having id mainMenu Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1].
//div[@id='mainMenu']/ul/liSelects all the li of ul which is child of div having id as mainMenu
//div[@id='mainMenu']/ul/li[last()]Selects the last li from above explained list of li
//a[text()='Home']Selects all the a(anchor) having text Home
//div[@id='mainMenu']/ul/li[position()<3]Selects the first two li from above explained list of li
On similar line we can use all below listed Predicates as per requirements.
position()Matches the particular position from the list
last()Matches the Last position from the list
text()Get the text node of element
contains(string1,string2)Matches if the string2 contains in string1
starts-with(string1,string2)Matches if the String1 starts with String2
ends-with(string1,string2)Matches if the String1 ends with String2
matches(string,pattern)Matches if the String1 match with given regular expression pattern
upper-case(string)Convert the string to Uppercase
lower-case(string)Convert the String to Lowercase

Selecting Unknown Elements

XPath wildcard character * can be used to select unknown Web elements.
Please contact me via cotact me page and send me queries and suggestions....



Stay cool... & Keep Automating.....
Post a Comment

Popular posts from this blog

Alternate Way for sendKeys

One of my friend called me yesterday, he had an interview on Selenium WebDriver and got confused on 1 Question.

What is an alternative for sendKeys Method in WebDriver? I also got stammered on this and thought in mind
Why the hell do we need this? There are below possible reason that I could find, When we need any alternative to sendKeys.
When the Text element is disabled or locked, sendKeys can not set the value in text field.
(in my opinion, this is not the correct way of Automation, because the element is locked or disabled by intend to not allow any text insertion)When we want to write huge text as input... that time the way WebDriver work, by sending Series of characters from String one by one, and Which is Very time consuming. so to minimize that time we can use this alternate method So now we have above two problems with sendKeys ......
Solution!!! The best alternative I found is JavaScript
Way 1 to Execute Script WebDriver driver = new FirefoxDriver(); driver…

Selecting Date on JQuery Datepicker

Hello Friends,

In this post we will see how to select specific date in JQuery Datepicker.

Its very easy to enter date as text by .sendKeys() method, but mostly when application uses JQuery Datepicker, it disables typing on the text-box. the text-box sets to read only.

While automating such screens we need to write logic which can interact with Datepicker and selects desired date.

Consider below example of Datepicker.


Html code for above example is


To automate this, we first break apart a problem.
Decide which date we want to set Read the current date from datepicker Calculate difference between both dates in Months(difference will positive in case of set date is in future respective to current date & will negative when set date is in past respective to current date of datepicker) Set loop depends on month difference and navigate the datepicker to specific month Select the Day. 
Lets implement the above logic.
Note: we have used Joda Date Time Library for calculate date and time …