How to use Xpath in selenium

Posted By :Preeti Choudhary |30th July 2023

Syntax of Xpath-

XPath = //tagname[@attribute='value']

// - It means Select the current node

tagname - tagname of the particular node

@ - Select Attribute

attribute - Attribute of the node

value -  value of the node

For an Example -

 

 

Types of Xpath Locators-

1.Absolute Xpath 

2.Relative Xpath

 

 

 

 

 

 

 

 

1.Absolute Xpath IT is the direct way to find out the element .It's start with a single Forward Slash (/)  and specific the complete path of an element from the root node .

For Example-

/html/body/div/div[2]/form/input[1]

Real Scenario - We have taken a ScreenShot above to show the Example of Absolute XPATH (login Page).

/html/body/div[1]/div[1]/div[1]/div[1]/div[1]/form/div[1]/div[1]/div[1]/div/[1]div[1]

2.Relative Xpath - It's Starts from the Middle of HTML DOM structure. It's start with a double Forward Slash (//) and allow the locate element based on their relationship to the other elements on the page.

For Example - 

//input[@id='username']

Real Scenario - We have a taken a ScreenShot above to show the example of Relative XPATH(login).

//div[@class='MuiInputAdornment-root MuiInputAdornment-positionStart MuiInputAdornment-outlined MuiInputAdornment-sizeMedium jss8 css-1a6giau']

HTML DOM Structure - The DOM(Document Object Model) is a programming interface that represents the Structure of HTML or XML Document.It's Provide way to access and manipulate the element and content of a web page.

Below Is the example of Absolute Xpath And Reletive Xpath Expression of the same Element Shown in the below.

 

 

Xpath Locators-

There  are many Xpath Locators , we can find out the Xpath of the Web

Xpath Locators Find Different Elements on the Web Page
ID To find the elemnt By ID
Classname To find the element By Class Name
Name To Find the element By Name
Link Text To find the element By the text of the Link
XPath IT's Required for finding the dynamic element 

 

 

 

 

 

 

 

 

 

 

 

Use -  From Above ScreeShot showing below XPATH . By ID, By Class and By Name.

By ID-

//input[@id=":r8:"]

By Class-

//input[@class='MuiOutlinedInput-input MuiInputBase-input MuiInputBase-inputSizeSmall MuiInputBase-inputAdornedStart css-162edyi']

By Name-

//input[@name='hospitalName']

 

Using Pycharm-

 

 

 

 

 

 

 

 

 

 


About Author

Preeti Choudhary

Preeti is an exceptionally skilled Quality Analyst with expertise in various testing methodologies, including Manual Testing for both web and mobile applications, as well as Automation Testing specifically for web applications. She is well-versed in API Testing, Database Testing, and CBT Testing. She has extensive experience working with bug-tracking tools like JIRA and Trello, enabling her to effectively track and manage issues throughout the testing process. She has made significant contributions to the success of various internal and client projects, including Enviuver, Heartbeat, OCR, TripCongo, ESP Safety OCR, and Hydroleap Website Development. Preeti's expertise in Manual Testing has allowed her to identify defects and ensure the smooth functionality of applications across both web and mobile platforms. She is dedicated to delivering high-quality work and ensuring the success of the projects she contributes to.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us