- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
We can find the xpath of the element that keeps changing with the help of the xpath functions. They help to identify an element having dynamic attribute value or text. Some of these functions are listed below −
text() – Identifies an element with the help of the visible text on the page. The xpath expression for the element Home is //*[text()='Home'].
starts-with – Identifies an element whose attribute value begins with a specific text. This function is normally used for attribute values which are dynamic in nature.
The xpath expression of Home is //a[starts-with(@title, 'Questions &')].
contains - Identifies an element whose attribute value contains a sub-text of the actual attribute value. This function is normally used for attribute values which are dynamic in nature.
The xpath expression of Home//a[contains(@title, 'Questions & Answers')].
We can also create a dynamic xpath with the help of single or multiple attributes with the format −
//tagname[@attribute='value'] – with one attribute //tagname[@attribute1='value'][@attribute2='value'] – with more than one attributes.
Syntax
//img[@alt='Tutorialspoint']
Syntax
//img[@alt='Tutorialspoint'] [@title='Tutorialspoint']
We can also create a dynamic xpath with the help of ORand AND operations on attributes with the format −
//tagname[@attribute1='value' or @attribute2='value'] //tagname[@attribute1='value' and @attribute2='value']
Syntax
//img[@alt='Tutorialspoint' or @name='Tutorial'] //img[@alt='Tutorialspoint' and @id='txt']
- Related Articles
- How to find an element using the “XPath” in Selenium?
- Find Element and FindElements by XPath in Selenium
- How to select element using XPATH syntax on Selenium for Python?
- How to identify nth element using xpath in Selenium with python?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- Unable to locate an element using xpath error in selenium-java
- How to identify the nth sub element using xpath?
- What is xpath in Selenium?
- What is xpath in Selenium with python?
- How do I select elements inside an iframe with Xpath?
- How to use text() in xpath in Selenium with python?
- Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium?
- What is the difference between relative and absolute XPath in Selenium?
- How to use regular expressions in xpath in Selenium with python?
- How to use xPath in Selenium WebDriver to grab SVG elements?
