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.
- It first tries to get the property value.
- If the property is not found, it looks for the HTML attribute.
- If neither exists, it returns None.
In this article, we'll learn how to use theget_attribute() method in Selenium to extract an element’s attribute value (like href, id, placeholder, etc).
Syntax
element.get_attribute("attribute_name")
Parameters:
- attribute_name: name of the attribute that needs to be fetched.
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.
Pythonfromseleniumimportwebdriver# Create WebDriver objectdriver=webdriver.Firefox()driver.get("https://www.geeksforgeeks.org/")el=driver.find_element("link text","DSA")print(el.get_attribute("href"))
Terminal Output:
Terminal outputOutputafter visiting the URL in the terminal:
DSA Tutorial pageExplanation:
- get() method opens the GeeksforGeeks homepage.
- find_element() method finds the Courses link using link text locator.
- get_attribute('href') fetches the destination URL of the anchor (<a>) tag.
Example 2: Get placeholder Attribute from an Input Field
In this example, we’ll extract the placeholder text from the Google search input box.
Pythonfromseleniumimportwebdriver# Initialize Firefox WebDriverdriver=webdriver.Firefox()driver.get("https://www.google.com/")box=driver.find_element("name","q")print(box.get_attribute("placeholder"))
Explanation:
- get() method navigates to Google's homepage.
- find_element("name", "q") locates the search input box using the name locator.
- get_attribute("placeholder")returns the default placeholder text, such as"Search".
Other Locator Examples
You can also locate elements using other strategies likeidorxpath.
Pythonel=driver.find_element("id","link")el=driver.find_element("xpath","//a[@id='link']")
Explanation:
- find_element("id", ...) locates elements using the id attribute.
- find_element("xpath", ...) allows more complex querying via XPath.
- These elements can then be passed toget_attribute() for attribute extraction.
Example 3: Get href of Multiple Anchor Tags
Here’s how to extract the href attribute from all anchor (<a>) tags on a page.
Pythonfromseleniumimportwebdriverdriver=webdriver.Firefox()driver.get("https://www.geeksforgeeks.org/")links=driver.find_elements("tag name","a")forlinkinlinks:print(link.get_attribute("href"))
Output:
Terminal output of all the hrefsExplanation:
- get() method opens the GeeksforGeeks site.
- find_elements() returns a list of all anchor tags (<a>).
- get_attribute("href") fetches each link's destination.
Related Articles: