Finding Elements by ID, Name, and Class with Python (Selenium)

Finding Elements by ID, Name, and Class with Python (Selenium)

When you write test scripts or automation with Selenium and Python, one of the first things you’ll need to do is find elements on a webpage — like buttons, inputs, and links.

Selenium provides multiple ways to do this. In this blog, we’ll cover the most commonly used ones:

  • By ID

  • By Name

  • By Class Name

Let’s get started!


πŸ›  Prerequisites

Make sure you have:

  • Python installed

  • Selenium installed

    pip install selenium
  • WebDriver for your browser (like ChromeDriver)


πŸ“˜ Basic Setup

Here’s a quick example setup using Chrome WebDriver:

from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://example.com")

Now let’s explore how to find elements.


πŸ”Ž 1. Find Element by ID

πŸ“Œ What is an ID?

An id is a unique identifier for an HTML element. It should be used when possible because it's fast and reliable.

✅ Example:

<input type="text" id="username">

🐍 Python Code:

username = driver.find_element(By.ID, "username") username.send_keys("myUser")

πŸ”Ž 2. Find Element by Name

πŸ“Œ What is a Name?

The name attribute is often used in forms and can also help locate elements.

✅ Example:

<input type="password" name="userPassword">

🐍 Python Code:

password = driver.find_element(By.NAME, "userPassword") password.send_keys("mySecret")

πŸ”Ž 3. Find Element by Class Name

πŸ“Œ What is a Class?

The class attribute is often used to style groups of elements.
Note: There may be multiple elements with the same class.

✅ Example:

<button class="submit-btn">Login</button>

🐍 Python Code:

login_button = driver.find_element(By.CLASS_NAME, "submit-btn") login_button.click()

⚠️ Things to Remember

  • find_element() returns the first match only.

  • If no element is found, Selenium throws a NoSuchElementException.

  • For multiple elements, use find_elements() (with an "s").


πŸ§ͺ Bonus: Wait for Elements

Sometimes, elements take time to load. Use WebDriverWait to avoid errors:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) username = wait.until(EC.presence_of_element_located((By.ID, "username")))

🧾 Summary Table

MethodUse When...Code Example
By.IDElement has unique iddriver.find_element(By.ID, "email")
By.NAMEElement has a name attributedriver.find_element(By.NAME, "password")
By.CLASS_NAMEElement has a common classdriver.find_element(By.CLASS_NAME, "btn")

✅ Final Thoughts

Finding elements is the foundation of browser automation and web testing.
Start with ID when available, fall back to Name or Class when needed.

With a good understanding of these basics, you’re ready to automate more complex tasks with Selenium and Python!


Read More

Exploring Selenium Web Element Methods in Python



Comments

Popular posts from this blog

What is WebDriver in Selenium? Explained with Java

Tosca System Requirements and Installation Guide (Step-by-Step)

Why Choose Python for Full-Stack Web Development