Different ways to write XPATH in Selenium
In this Blog i will explain about different ways to use XPATH in selenium.
Read along with me and practice to know more about xpath with me.
There are 6 concepts covered on XPATH which will really help you to find the Web Elements on the Web Page.
Before into the concept of xpath overall we need to know why exactly we are need to find the xpath in selenium.
xpath is nothing but a unique ID or an address of aweb element on the web page.
By using xpath expression we can uniquely identify the web element and perform the required actions and automate.
What is xpath in Selenium:
xpath in selenium is that allows us to navigate the structure of Webpage HTML code.
xpath is nothing but syntax for finding elements on the webpage.It can be used on both HTML and XML documents.
The elements which can not be found by using ID,classname,tagname,LinkText then we will use Xpath Expression to find the Web Element on the web page.
What is xpath?
xpath is nothing but the XML path.It is a syntax or language for finding out the elements on the web page using xpath. It is used to find the loacation of any element using HTML DOM structure. In Selenium automation ,if you are unable to find the elements using ID,ClassName then we use xpath for finding the elements. The Basic format of xpath is: //tagname[@attributename='value'] I will Explain about the syntax now . // is used to select the current node on HTML. .Tagname is the name of the particular node. .@ is used to select the attribute. Attribute is the name of attribute of the node. .Value is used for the value of the attribute.
Types of Xpath locators:
In order to find the xpath we have 8 different types of loactors.Here are the few examples below:
Xpath Locators 1)ID To find the element by ID of the element. Syntax: driver.findElement(By.id(<element ID>)) 2)Classname To find the element by classname of the element. Syntax: driver.findElement(By.classname(<element name>)) 3)Name To find the element by name of the element Syntax: driver.findElement(By.name(<element name>)) 4)Tag name
To Find the element by its tagname Few tags in html which we find on the html page
Tag in HTML
Text Box, Radio Button, Check Box
Syntax: driver.findElement(By.tagname(<element name>))
To find the element by link text
Syntax: driver.findElement(By.linktext(<element name>))
6)Partial Link Text
To find element by partial link text
Locates a link by using link's partial text
Syntax: driver.findElement(By.partiallinktext(<element name>))
To find element with xpath
Xpath is required for finding dynamic element and traverse between various elements of the web page.
Syntax: driver.findElement(By.xpath(<element name>))
To find element using css selector
CSs selector also locates elements having no id,class,name
Syntax: driver.findElement(By.cssselector(<element name>))
Xpath is of two types
Now i will explain Absolute Xpath:
Absolute Xpath is using Single slash at the start of the path.
A single slash at the start of the xpath instructs Xpath engine to look for element starting from root node.
A single slash'/' anywhere in the xpath signifies to look for the element immediately inside its parent element.
In order to locate the element, you can simply do a right-click on the web element and click on Inspect. Then, in the Elements tab, you can start writing the locator.
Xpath uses Double slash at the starting of xpath.
ADouble slash at the starting of Xpath instructs Xpath engine to search look for matching element anywhere on the XML document.
ADouble slash'//' signifies to look for any child or any grand-child element inside the parent element.
Differnt ways to write Dynamic Xpath
This is the common and syntactic approach to writing the XPath in Selenium, which combines a tagname and attribute value.Xpath expression selects nodes on the basis of attribute like Classname,ID,name..XPath.
Here are some more basic Xpath expressions
2.Xpath using Tagname and contains()
Contains() is a method used in Xpath expression.It is used when value of an attribute changes dynamically.
Using Logical Operators OR and AND
In OR expression,Two conditions are used,whether the first condition OR second condition should be true.
It is also applicable when one condition is true or both the conditions are true.
Below is the example for DEMOQA page email with OR expression
Using And expression
Xpath using Text()
The text() method is used in XPath whenever we have a text defined in an HTML tag, and we wish to identify that element via text. This comes in handy when the other attribute values change dynamically with no substantial attribute value used via Starts-with or Contains.
Syntax: //tagname[text()='text of the web element']
xpath using Starts-with()
The Starts-With() method is similar to the Contains() method. It is helpful in the case of web elements whose attribute value can change dynamically. In the Starts-With method, the starting value of the attribute’s text is used for locating the element.
Here, the placeholder attribute of the FirstName textbox contains a lot of characters. However, it starts with the word firstName. Hence, by using the starts-with method, you can simply locate the element in this case.