Last Updated : 12 Jul, 2025
Selenium is a powerful Python module used for browser automation. It allows you to interact with web pages just like a real user- click buttons, fill forms, and fetch values from elements.
The get_attribute() method fetches the value of an element’s HTML attribute.
In this article, we'll learn how to use the get_attribute() method in Selenium to extract an element’s attribute value (like href, id, placeholder, etc).
Syntaxelement.get_attribute("attribute_name")
Parameters:
Return Type: the attribute value as a string, or None if the attribute is not found.
InstallationInstall the selenium module using pip, use this command:
Examples of get_attribute() Example 1: Get href from a Linkpip install selenium
Let's start by retrieving the href attribute of a link element on the GeeksforGeeks homepage.
Python
from selenium import webdriver
# Create WebDriver object
driver = webdriver.Firefox()
driver.get("https://www.geeksforgeeks.org/")
el = driver.find_element("link text", "DSA")
print(el.get_attribute("href"))
Terminal Output:
Terminal outputOutput after visiting the URL in the terminal:
DSA Tutorial pageExplanation:
In this example, we’ll extract the placeholder text from the Google search input box.
Python
from selenium import webdriver
# Initialize Firefox WebDriver
driver = webdriver.Firefox()
driver.get("https://www.google.com/")
box = driver.find_element("name", "q")
print(box.get_attribute("placeholder"))
Explanation:
You can also locate elements using other strategies like id or xpath.
Python
el = driver.find_element("id", "link")
el = driver.find_element("xpath", "//a[@id='link']")
Explanation:
Here’s how to extract the href attribute from all anchor (<a>) tags on a page.
Python
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.geeksforgeeks.org/")
links = driver.find_elements("tag name", "a")
for link in links:
print(link.get_attribute("href"))
Output:
Terminal output of all the hrefsExplanation:
Related Articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4