Saturday, 26 January 2019

XPath in Selenium Webdriver


Automation 
involves identifying the elements on the UI and performing actions on them, so in order to identify the UI elements there are various locators available on the HTML DOM  by means of which Selenium WebDriver is able to identify and automate them.
In some cases if these elements are not found using other locators such as Id, Class, Css etc then Xpaths are used.

What is Xpath?

XPath (XML Path Language) is a query language for selecting nodes from an XML document.
Xpath language provides the ability to navigate around XML tree and select the  nodes by various criteria.
Below is an example of XML tree:




In this example Product is the Root Element of the tree and the two child nodes of Product are Name and Detail

Understanding HTML Dom
Whenever a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM is a standard object model and programming interface for HTML. It defines:
The HTML elements as objects
The properties of all HTML elements
The methods to access all HTML elements
The events for all HTML elements

In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements.
The HTML DOM is constructed as a tree of objects:


In order to start studying about xpath it is very important to have some basic knowledge about the DOM structure.

Consider the below HTML DOM of facebook signup page.



Let us concentrate on the highlighted portion of the DOM:
Here "input" is a tag and "class,data-type,name,aria-required,placeholder aria-label and id" are known as attributes.Each of these attributes have some values which are used for creating the html page.

When should xpath be used:

Elements on any UI can be located using various html attributes such as ID,Class,name etc,but many a times it happens that:
1. The attributes that are supported by selenium webdriver are not present 
2. The attributes that are available are not unique.

In such cases xpaths can be used to locate the UI elements. Xpaths can easily traverse through the HTML nodes and this feature provides a lot of flexibility to create unique identifiers
.

Basic syntax of xpath:

//tagname[@attribute_name='attribute_value']

Explanation:
//-Selects nodes in the document from the current node that match the selection no matter where they are

@-Selects attribute

Let us look at a simple example below






We have created an xpath for locating first name text box by using the basic syntax-//tagname[@attribute_name='attribute_value']

In the above created xpath input is the tag name,name is the attribute and 'firstname' is the value of the attribute name.

Typing the above xpath in the html dom highlights the HTML element that points to firstname textbox on the UI which assures that the created xpath is a correct one.

 


0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses