The getAttribute() Function in Selenium Python

The getAttribute() Function in Selenium Python

  1. the getAttribute() Function in Selenium Python
  2. Advantages of the getAttribute() Function in Selenium Python

The Python module for Selenium is developed to deliver an automated testing process. The Selenium Python bindings include a simple API for writing Selenium WebDriver functional/acceptance tests.

Having the ability to move is not much beneficial. We want to interact with the pages, or more precisely, the HTML pieces consisting of a page.

This article will explain using Selenium’s getAttribute() method.

the getAttribute() Function in Selenium Python

The getAttribute() method can retrieve element properties, such as an anchor tag’s href attribute. This function will initially attempt to return the value of a specified property.

If no such property exists, the attribute’s value with the same name is returned. However, none is returned if no attributes exist in an element.

What Is an Attribute

An attribute in HTML defines the properties of an element. Attributes comprise name-value pairings, which means that each attribute has a value and a name.

Their values are encased in double quotes and are provided in the opening tag. HTML attributes include the following:

< a target="_blank" href="#" class="btn" >

<img src="#" class="image" alt="myImage" >

< input style="height:50px;" class="form-control" type="email" placeholder="Enter email" name="emailAddress" id="emailAddress" >

< button onclick="#" class="btn" value="" >Login< /button >

As from the above example, HTML tags get a variety of properties and values. For example, the input tag had many attributes such as class, style, placeholder, type, name, and id.

There is a number in double quotes for each property. Now that we understand what an attribute means for an HTML web element or how it appears let’s check why the getAttributes() method is useful.

Advantages of the getAttribute() Function in Selenium Python

Consider a situation where we must double-check the placeholder content on an input field, the picture source, and the field size. The getAttribute() method solves this problem in this situation.

To obtain the value of an attribute, locate the web element that holds it and use the getAttribute() method.

Let’s discuss the syntax of this method in a real example, as shown below.

# python


As you can see from the above syntax, we are trying to get the href attribute. Now, let’s go through a working example of fetching values using the getAttribute() method now that we’ve learned the basic syntax for utilizing it.

To develop a basic test script, we’ll utilize a dummy website. Without further hemming and hawing, let’s look at the use case before moving on to the code:

  1. We will go to the dummy site first.
  2. On the Homepage Front-End, we will get a link’s href attribute.
  3. We will also get an image’s src attribute.

Now, let’s look at the code for this use case.

Example code:

# python
from selenium import webdriver

chromeDriver = webdriver.Chrome()


getElemByLink = chromeDriver.find_element_by_link_text("About Us")


getElemByClass = chromeDriver.find_element_by_xpath("/html/body/main/div/div[1]/section[1]/div/div/div/div[7]/div/img")



Selenium getAttribute() Example in Python

As we can see from the above example, we can get any attribute from an HTML tag using the getAttribute() method. This method can help us in web scraping to get specific data from a website, such as links and images.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.


Related Article - Python Selenium

  • Check if Element Exists Using Selenium Python
  • Python Selenium Refresh Page
  • WebDriverException: Message: Geckodriver Executable Needs to Be in PATH Error in Python
  • Scroll Down Web Page Using Selenium in Python
  • Install Python Selenium in macOS
  • Login to a Website Using Selenium Python