The Wayback Machine - https://web.archive.org/web/20240613150120/https://www.geeksforgeeks.org/get_attribute-element-method-selenium-python/
Open In App

get_attribute() element method – Selenium Python

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. There are multiple strategies to find an element using Selenium, checkout – Locating Strategies
This article revolves around how to use get_attribute method in Selenium. get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag. This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.
Syntax – 
 

element.get_attribute("attribute name")

Example – 
 

html




<a href="https://www.geeksforgeeks.org/" id="link" />


To find an element one needs to use one of the locating strategies, For example,
 

element = driver.find_element_by_id("link")
element = driver.find_element_by_xpath("//a[@id='link']")

Also, to find multiple elements, we can use – 
 

elements = driver.find_elements_by_id("link")

Now one can get attribute of this field with 
 

element.get_attribute('href')

 

How to use get_attribute method in Selenium Python ?

Let’s use https://www.geeksforgeeks.org/ to illustrate this method in Selenium Python . Here we get href attribute of courses tab in navigation bar at geeksforgeeks. 
Program – 
 

Python3




# import webdriver
from selenium import webdriver
 
# create webdriver object
driver = webdriver.Firefox()
 
# enter keyword to search
keyword = "geeksforgeeks"
 
# get geeksforgeeks.org
 
# get element
element = driver.find_element_by_link_text("Courses")
 
# get href attribute
print(element.get_attribute('href'))


Output- 
 

click-element-method-Selenium-Python

Terminal Output – 
 

get_attribute-element-method-Selenium-Python

 



Previous Article
Next Article

Similar Reads

How to use Selenium and Selenium WebDriver Manager to Login to a Website with Python?
Selenium is an open-source tool that is used for automating the tests carried out on different web browsers. Selenium WebDriver is a web framework that allows executing cross-browsing testing. This article focuses on discussing how to log in to a website with Python using Selenium and Selenium WbDriver Manager. Table of Content What is Selenium?Wha
5 min read
What is the Difference Between Selenium Core Extensions and Selenium IDE Extensions?
Selenium is a popular tool for testing and automating web applications. It has changed over time and has different parts that help users in different ways. The two important parts are Selenium Core (from the past) and Selenium IDE. Both have extensions that add additional features to the tool. This article looks at the differences between Selenium
8 min read
Difference Between Selenium 2.0 and Selenium 3.0
Selenium is a leading device for net utility trying out in terms of software program checking out and automation. Selenium has developed through the years, with foremost improvements and upgrades to be had in variations 2.0 and 3.0. Software program testers must recognize the differences between Selenium 2.0 and 3.0 to successfully make use of the
6 min read
Selenium vs Cypress - Which Framework is better to learn Selenium or Cypress?
This article focuses on discussing the key considerations when choosing between Cypress and Selenium for Automation testing. We'll compare their features, benefits, and limitations, helping one determine which framework aligns best with the testing requirements and objectives. Table of Content What is Selenium?Advantages of SeleniumDisadvantages of
5 min read
What is the main difference between Selenium 1 and Selenium 2?
Selenium is a suite of software widely used in the field of automation and testing, it has been a key player in the field of automation for a long time. This article will deep into Automation and discuss the difference between Selenium version 1 and Selenium version 2, highlighting their differences, advantages, and disadvantages. Whether the user
10 min read
Differences Between Selenium Standalone server and Selenium Client and WebDriver Server
In web application testing, Selenium has emerged as one of the most basic foundations, providing robust mechanisms for automating browsers. Among its suite, two elements stand out, which are: 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver'. Both 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver' are very impor
4 min read
is_selected() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
is_displayed() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
is_enabled() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
get_property() element method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
Practice Tags :