top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Different ways to write XPATH in Selenium

By


Aishwarya Kopperapu


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:


Tag in HTML

Purpose

a

Link

b

bold

table

Table

tr

Row

td

Column

select

Dropdown

button

Button

span

Plain Text

input

Text Box, Radio Button, Check Box

img

image

i

italic

iframe

frame

div

Ajax Control


Syntax: driver.findElement(By.tagname(<element name>))


5)Link Text


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>))


7)Xpath


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>))


8)Css selector


To find element using css selector

CSs selector also locates elements having no id,class,name

Syntax: driver.findElement(By.cssselector(<element name>))



Xpath Examples:


Xpath is of two types


*Absolute Xpath

*Relative Xpath


Now i will explain Absolute Xpath:

  1. Absolute Xpath is using Single slash at the start of the path.

  2. A single slash at the start of the xpath instructs Xpath engine to look for element starting from root node.

  3. 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.


Example:/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[2]/div[2]/form[1]/div[3]/div[2]/div[1]/label[1]




Relative Xpath:


  1. Xpath uses Double slash at the starting of xpath.

  2. ADouble slash at the starting of Xpath instructs Xpath engine to search look for matching element anywhere on the XML document.

  3. ADouble slash'//' signifies to look for any child or any grand-child element inside the parent element.

Example:

//input[@id='userNumber']



Differnt ways to write Dynamic Xpath


  1. Basic 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.


Example:Xpath=//*[@name='firstname']





Here are some more basic Xpath expressions


Xpath=//input[@name='firstName']

2.Xpath using Tagname and contains()

for RadioButton




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.


Syntax: //tagname[starts-with(@attribute,value)]


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.








49 views0 comments
bottom of page