- 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
How to use relative xpath for locating a web-element by particular Attribute in Selenium?
We can use relative xpath for locating web-element by particular attribute value. A relative xpath begins from the element to be located and not from the root.
It begins with the // symbol. It’s advantage is that even if an element is deleted or added in the DOM, the relative xpath for a specific element remains unaffected. To obtain a relative path by an attribute, the xpath expression is //tagname[@attribute='value'].
Let us identify the below highlighted element on the page with the help of the alt attribute.
Syntax
l = driver.find_element_by_xpath("//img[@alt='tutorialspoint']")
Example
from selenium import webdriver #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) #launch URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #identify element with an attribute using xpath l = driver.find_element_by_xpath("//img[@alt='tutorialspoint']") #get src attribute s = l.get_attribute('src') print('Src attribute value is: ') print(s) #browser quit driver.quit()
Output
- Related Articles
- Find Element and FindElements by XPath in Selenium
- How to select element using XPATH syntax on Selenium for Python?
- How to use Selenium WebDriver for Web Automation?
- The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- How to find an element using the “XPath” in Selenium?
- How to use text() in xpath in Selenium with python?
- What is the difference between relative and absolute XPath in Selenium?
- How to use xPath in Selenium WebDriver to grab SVG elements?
- How to use selenium to check if element contains specific class attribute?
- How to identify nth element using xpath in Selenium with python?
- How to use regular expressions in xpath in Selenium with python?
- Why use Selenium WebDriver for Web Automation?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- Selenium Webdriver Locating Strategies By Class Name

Advertisements