CodeGym /Java Course /Python SELF EN /Finding Elements by HTML Attributes: id, class

Finding Elements by HTML Attributes: id, class

Python SELF EN
Level 35 , Lesson 3
Available

1. Basics of Finding Elements

Methods for Finding Elements

Before diving headfirst into code, it's important to understand how we can locate elements on a webpage using Selenium. Selenium offers several handy methods for finding elements:

  • find_element_by_id(): Finds an element by its unique identifier (id).
  • find_element_by_name(): Lets you find an element by its name attribute.
  • find_element_by_class_name(): Finds elements with a specific class (class).
  • find_element_by_tag_name(): Uses the tag name to locate elements, like div, p, a.

Let's start with a simple example — finding an element by its id. Imagine we've lost a button labeled "Magic Button," and we need to find it:

Python

from selenium import webdriver

# Setting up the driver and opening the browser
driver = webdriver.Chrome()
driver.get('https://example.com')

# Finding an element by its id
magic_button = driver.find_element_by_id('magic_button_id')

# Working with the found element (e.g., extracting text)
print(magic_button.text)

# Ending the session
driver.quit()

Working with Found Elements

Once we've found an element, it's time to decide what to do with it. Get creative and use Selenium methods like text to grab the text content of the element or get_attribute('attribute_name') to extract the value of any element attribute.

Let's say our element is a link, and we want to know where it leads. Here's how we do it:

Python

# Finding an element with a link
link_element = driver.find_element_by_class_name('link_class')

# Extracting the URL from the href attribute
link_url = link_element.get_attribute('href')
print(f'This link goes to: {link_url}')

2. The find_element_by_id() Method

The find_element_by_id() method is one of the fastest and most reliable ways to locate an element on a webpage. It allows you to find elements by their unique id attribute. In HTML, the id attribute is unique, so you can be sure that this method will return the one element you need.

Example Usage:

Python

element = driver.find_element_by_id("submit-button")

In this example, Selenium searches for an element with id="submit-button" and saves it in the element variable. This approach is convenient for locating buttons, forms, input fields, and other unique elements that are rarely duplicated on a page.

When to Use?

Using find_element_by_id() is recommended whenever an element has a unique id attribute, as this method provides quick and accurate access to the element.

3. The find_element_by_name() Method

The find_element_by_name() method locates elements by their name attribute. It's often used for forms and input fields, especially when elements may share the same class or id but have different name attributes. Unlike id, the name attribute is not necessarily unique, and sometimes there may be multiple elements with the same name on a page.

Example Usage:

Python

element = driver.find_element_by_name("username")

This code finds an element with name="username". Typically, this would be a field for entering a username in a login form.

When to Use?

This method is useful if an element on the page doesn't have a unique id but does have a name attribute. It's especially effective for input forms, where elements are often identified by name.

4. The find_element_by_class_name() Method

The find_element_by_class_name() method locates an element by its class (class). It's handy when working with CSS and allows you to find elements with the same styling. It's worth noting that a class can be assigned to multiple elements, so if you need to find just one element, it's important to specify a unique class or use another method.

Example Usage:

Python

element = driver.find_element_by_class_name("main-header")

In this example, Selenium looks for the first element with the class main-header.

When to Use?

find_element_by_class_name() is often used to find headers, buttons, and other styled elements. This method is especially useful if elements lack unique id or name attributes, or when you need to locate elements based on their styling.

5. The find_element_by_tag_name() Method

The find_element_by_tag_name() method locates elements by their tag name, such as div, p, a, input. It's useful for getting all elements of a certain type, especially if the structure of the page makes it easy to navigate by tags.

Example Usage:

Python

element = driver.find_element_by_tag_name("h1")

Here, Selenium searches for the first element with the h1 tag, which is typically used for page headings.

When to Use?

The find_element_by_tag_name() method is a good choice for finding headings, images, paragraphs, and other elements that are represented by a specific tag type on a page. However, since there may be many elements of the same tag type, this method doesn't always pinpoint a specific element.

6. Features and Errors

Before we move on, let's take a moment to appreciate the diversity of HTML. Sometimes elements like buttons or images might not have unique ids or classes. In such cases, more advanced locating methods like XPath and CSS selectors come to the rescue, which we'll discuss in the next lecture. But if you encounter such elements now, don't panic. It's all part of the fun in the world of web automation.

Fragility of Selectors

When working with attributes like id and class, remember that they can change. Selecting an element by its id, which unexpectedly changes, might lead to an error. This is especially true for dynamically generated pages, where identifiers can be random. Solution? Use more stable attributes or combined approaches.

7. Practical Application

Now we know how to find elements by id, class, and name. This knowledge is super valuable, especially when it comes to automating interactions with web pages. Successfully located elements can be used to fill out forms, click buttons, or extract information — actions that can significantly speed up your work and free you from monotonous tasks.

Imagine a company where every employee spends 10 minutes a day filling out the same form on a website. And then Selenium comes to the rescue: it wraps up all the grunt work in minutes while you enjoy your morning coffee.

For those already intrigued by real-world cases, Selenium automation is used for testing web applications, monitoring website updates, and even for quickly recovering a forgotten password on your favorite social network if their algorithms decide you don't look like yourself in those panda photos.

1
Task
Python SELF EN, level 35, lesson 3
Locked
Find an Element by Unique Identifier
Find an Element by Unique Identifier
2
Task
Python SELF EN, level 35, lesson 3
Locked
Finding and extracting a URL from an element by class
Finding and extracting a URL from an element by class
3
Task
Python SELF EN, level 35, lesson 3
Locked
Finding and Interacting with a Button
Finding and Interacting with a Button
4
Task
Python SELF EN, level 35, lesson 3
Locked
Finding elements and working with them inside a table
Finding elements and working with them inside a table
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION