A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://geeksforgeeks.org/get_attribute-element-method-selenium-python/ below:

get_attribute() element method - Selenium Python

get_attribute() element method - Selenium Python

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).

Syntax

element.get_attribute("attribute_name")

Parameters:

Return Type: the attribute value as a string, or None if the attribute is not found.

Installation

Install the selenium module using pip, use this command:

pip install selenium

Examples of get_attribute() Example 1: Get href from a Link

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 output

Output after visiting the URL in the terminal:

DSA Tutorial page

Explanation:

Example 2: Get placeholder Attribute from an Input Field

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:

Other Locator Examples

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:

Example 3: Get href of Multiple Anchor Tags

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 hrefs

Explanation:

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