- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is xpath in Selenium?
Xpath is one of the most important locators used in Selenium for identifying web elements. It works in the following ways −
It navigates through the Document Object Model (DOM) with the help of elements and their attributes for identification.
Although it helps to locate the elements uniquely, it is slower in terms of speed from the other locators.
An xpath is represented by two ways namely ‘/ ‘and ‘// ‘. A forward single slash means absolute path. Here xpath traverses direct from parent to child in DOM. Thus in absolute xpath we have to travel from the root node to the target.
Syntax −
driver.findElement(By.xpath("/html/body/div/input")).
A double forward ‘// ‘slash means relative path. Here xpath finds the matching element in every corner of DOM. It doesn't have a particular beginning point.
Syntax −
driver.findElement(By.xpath("//input[@name=’Tutorial’]")).
It is always recommended to use relative xpath rather than absolute xpath. In absolute xpath, we need to specify from the root to the desired element so if any of the attributes and its value get changed in between, then our xpath will no longer be correct.
Syntax for xpath −
//tagname[@attribute=’value’] or //*[@attribute=’value’]
Xpath basically locates elements with the help of XML path. XML known as the Extensible Markup Language.
- Related Articles
- What is xpath in Selenium with python?
- What is the difference between relative and absolute XPath in Selenium?
- What is the primary difference between the XPath and CSS selector in Selenium?
- Find Element and FindElements by XPath in Selenium
- What are the differences between xpath and css in Selenium with python?
- How to use text() in xpath in Selenium with python?
- How to find an element using the “XPath” in Selenium?
- How to use regular expressions in xpath in Selenium with python?
- The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
- How to identify nth element using xpath in Selenium with python?
- How to use xPath in Selenium WebDriver to grab SVG elements?
- Unable to locate an element using xpath error in selenium-java
- Clicking on elements within an SVG using XPath (Selenium WebDriver).
- Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium?
- How to select element using XPATH syntax on Selenium for Python?
