- 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
How to get attribute of element from Selenium?
We can get the attribute of element in Selenium webdriver. The getAttribute() method is used to obtain the value of an attribute in an html document. In an html code, attribute and its value appear as a key value pair.
Some of the commonly known html attributes are disabled, alt, id, href, style, title and src. The value of the attribute that we want to fetch is passed as an argument to the method.
Let us consider an html code, for which we shall obtain the src attribute. The value of the src attribute should be /about/images/logo.png.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class GetSrcAttribute{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String u = "https://www.tutorialspoint.com/about/about_careers.htm"driver.get(u); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element WebElement t=driver.findElement(By.xpath("//img[@class='tp-logo']")); // get src attribute with getAttribute() System.out.println("Src attribute is : " + t.getAttribute("src")); driver.close(); } }Output
- Related Articles
- How to get an attribute value of an element in Selenium Webdriver?
- How to get text from Selenium element WebElement object?
- How to get an attribute value from a href link in selenium?
- How to get the attribute value of a web element in Selenium (using Java or Python)?
- How to extract the attribute value of an element in Selenium?
- How to find an element using the attribute “id” in Selenium?
- How to find an element using the attribute “name” in Selenium?
- How to use selenium to check if element contains specific class attribute?
- How to find an element using the attribute “class name” in Selenium?
- How to get coordinates or dimensions of element with Selenium Python?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to find an element using the attribute “HTML tag name” in Selenium?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- How to get entered text from a textbox in selenium?

Advertisements