Tuesday, 28 May, 2024
HomeProgrammingPythonMastering Selenium Python: From Beginner to Advanced Automation Expert – Lesson 3:...

Mastering Selenium Python: From Beginner to Advanced Automation Expert – Lesson 3: Locating Web Elements with Selenium

In this lesson, we will dive into various strategies for locating elements within a webpage using Selenium. Effective element location is essential for performing interactions like clicking buttons, entering text, and retrieving data from web pages.

Understanding Elements and the Document Object Model (DOM)

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. Selenium interacts with the DOM to identify web elements and perform actions.

1. Finding Elements by ID and Name

ID: The ID of an element is supposed to be unique within a page and is the most reliable way to locate an element quickly.

from selenium.webdriver.common.by import By

# Find an element by ID
element_by_id = driver.find_element(By.ID, "uniqueId")

This code snippet locates an element by its ID. You use By.ID to specify the type of locator strategy followed by the actual ID value as a string.

Name: Elements can also have a name attribute, which is often used in forms.

# Find an element by name
element_by_name = driver.find_element(By.NAME, "emailField")

This method locates an element by the name attribute, useful for forms where multiple elements might share the same name, like checkboxes or radio buttons.

2. Understanding Dynamic IDs and Handling Exceptions

Dynamic IDs are IDs generated each time the page loads, which can change on every page load. To handle dynamic elements, you can use partial matches in CSS selectors or XPath.

from selenium.common.exceptions import NoSuchElementException

    # Assume the ID starts with 'login_' but has dynamic characters following
    dynamic_element = driver.find_element(By.CSS_SELECTOR, "[id^='login_']")
except NoSuchElementException:
    print("Element not found")

This example uses a CSS selector to find an element whose ID starts with ‘login_’. The caret symbol ^ indicates the start of a value in CSS.

3. Finding Elements by XPath and CSS Selectors

XPath: XPath is a language for finding nodes in XML documents, which is also used in HTML documents to navigate the structure.

# Find an element using a complex XPath
element_by_xpath = driver.find_element(By.XPATH, "//div[@class='container']//button[@type='submit']")

This XPath locates a button within a div with the class ‘container’. It uses // to select nodes in the document from the current node that match the selection no matter where they are.

CSS Selectors: CSS selectors define patterns used to select the elements you want to style.

# Find an element using a CSS selector
element_by_css = driver.find_element(By.CSS_SELECTOR, "div.user-panel.main input[name='login']")

This CSS selector finds an input element within a div with the class ‘user-panel main’ where the name attribute is ‘login’.

4. Finding Elements by Link Text and Partial Link Text

Link text is the visible text in a hyperlink. Selenium can identify links by their text content.

# Find a link by its complete text
link = driver.find_element(By.LINK_TEXT, "View Profile")

# Find a link by partial text
partial_link = driver.find_element(By.PARTIAL_LINK_TEXT, "View")

This method is straightforward when you know the text within a hyperlink. By.LINK_TEXT is used for full matches, while By.PARTIAL_LINK_TEXT matches substrings of the link text.

5. Finding Elements by Class Name and Tag Name

Class Name: For elements defined by their class attributes.

# Find elements by class name
element_by_class = driver.find_element(By.CLASS_NAME, "buttonClass")

Tag Name: If you want to retrieve elements by their tag type.

# Find elements by tag name
element_by_tag = driver.find_element(By.TAG_NAME, "div")

These methods are useful when specific attributes like IDs and names are not available.


Knowing how to accurately locate elements is foundational for effective automation. This lesson has outlined several strategies provided by Selenium to target elements based on their attributes and their position within the DOM. As you practice these techniques, you’ll become more proficient in crafting robust automation scripts that can navigate and interact with complex web pages.

Series Navigation<< Mastering Selenium Python: From Beginner to Advanced Automation Expert – Lesson 2: Opening Different Browsers Using Selenium Python
Related articles

Most Popular