- 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 get an attribute value of an element in Selenium Webdriver?
We can get an attribute value of an element in the Selenium Webdriver. This is achieved with the help of the getAttribute method. In an html document, each element is identified with its tagname along with the element attributes with their values. To get an attribute value, we have to pass the element attribute as an argument to the getAttribute method.
Let us see the html code of an element and obtain the value of its src attribute. The value of its src attribute shall be /about/images/logo.png.
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class AttributeVal{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //application launch driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement l=driver.findElement(By.className("tp-logo")); // getAttribute() to get src value String value = l.getAttribute("src"); System.out.println("Src attribute is: "+ value); //browser close driver.close() } }
Output
- Related Articles
- How to verify an attribute is present in an element using Selenium WebDriver?
- How to extract the attribute value of an element in Selenium?
- How to get an attribute value from a href link in selenium?
- How to get attribute of element from Selenium?
- How to get an attribute value in jQuery?
- How to get all descendants of an element using webdriver?
- Finding an element in a sub-element in Selenium Webdriver.
- How to find an element using the attribute “id” in Selenium?
- How to find an element using the attribute “name” 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 attribute “class name” in Selenium?
- Verifying whether an element present or visible in Selenium Webdriver
- Test if an element is focused using Selenium Webdriver.
- How to find an element using the attribute “HTML tag name” in Selenium?
- How can I get the current contents of an element in webdriver?

Advertisements