- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
l = driver.find_element_by_xpath("//img[@alt='tutorialspoint']")
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()
- Find Element and FindElements by XPath in Selenium
- How to use Selenium WebDriver for Web Automation?
- How to select element using XPATH syntax on Selenium for Python?
- The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
- Selenium Webdriver Locating Strategies By Class Name
- Why use Selenium WebDriver for Web Automation?
- What is the difference between relative and absolute XPath in Selenium?
- How to use text() in xpath in Selenium with python?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- 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?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- How to verify color of a web element in Selenium Webdriver?