How to import and use selenium.webdriver.common.by.By

When it comes to automated testing and web scraping, Selenium has emerged as a powerful tool for web developers and testers. Selenium offers a comprehensive set of features that allow users to interact with web browsers programmatically. In this article, we will delve into the ins and outs of the selenium.webdriver.common.by.By module, a fundamental part of the Selenium library.

We will explore its purpose, demonstrate its usage with examples, and highlight common errors that programmers may encounter.



Understanding selenium.webdriver.common.by.By:

The “selenium.webdriver.common.by.By” module provides a set of locator strategies used to identify elements on a web page. It serves as a part of the Selenium WebDriver package, enabling users to interact with various web elements such as buttons, text fields, dropdowns, and more.

Locator Strategies:

The “By” class within the “selenium.webdriver.common.by” module is not just a module, but an enumeration (enum) in Python. An enumeration is a collection of named values, often referred to as members or enumerators. In the case of the “By” enum, it consists of various locator strategies, each designed to locate elements based on different attributes.

By utilizing the “By” Enum, you gain access to a set of predefined locator strategies, making it easier to specify the search criteria for identifying web elements. These locator strategies enable you to interact with elements based on their attributes such as ID, class name, CSS selector, XPath, tag name, link text, or partial link text.

Let’s take a look at some examples show-casing all the available locater strategies in this Enum. The below examples assume you already have Selenium and a Webdriver setup. If not, you can refer to this setup guide on Selenium.

Example 1: Locating an element by its ID:

Locates elements based on their unique identifier (id attribute).

from selenium.webdriver.common.by import By 

element = driver.find_element(By.ID, 'element_id')

Example 2: Locating an element by its Class Name:

Class Name: Locates elements using their CSS class name.

from selenium.webdriver.common.by import By 

element = driver.find_element(By.CLASS_NAME, 'class_name')

Example 3: Locating an element by its Tag Name:

Identifies elements by their HTML tag name.

from selenium.webdriver.common.by import By 

element = driver.find_element(By.TAG_NAME, 'tag_name')

Example 4: Locating an element by its Link Text or Partial Link Text:

Locates hyperlinks based on their exact or partial visible text.

from selenium.webdriver.common.by import By 

link = driver.find_element(By.LINK_TEXT, 'Click here') 
partial_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'here')

Example 5: Locating an element using a XPath expression:

Uses XPath expressions to locate elements based on their path within the document.

from selenium.webdriver.common.by import By 

element = driver.find_element(By.XPATH, '//input[@id="email"]')

Example 6: Locating an element using CSS Selectors:

Uses CSS Selectors to locate elements based on their path within the document. These are quite similar to XPath expressions, but generally less powerful, with the trade off of being slightly easier to use and learn.

from selenium.webdriver.common.by import By

element = driver.find_element(By.CSS_SELECTOR, "div.myClass")

Example 7: Locating an element using the Name attribute

Finds HTML elements based on their “name” attribute.

element = driver.find_element(By.NAME, 'element_name')

Common Errors with selenium.webdriver.common.by.By

While working with the By module, programmers may encounter some common errors and pitfalls. Let’s shed light on a few of these:

Forgetting to import the By module: One common mistake is neglecting to import the By module. Ensure that you have the following import statement at the beginning of your script:

from selenium.webdriver.common.by import By

Capitalization issues: The By module’s methods, such as By.ID or By.CSS_SELECTOR, are case-sensitive. Make sure you use the correct capitalization to avoid attribute errors.

from selenium.webdriver.common.by import by   # Wrong
from selenium.webdriver.common.by import By   # Right

element = driver.find_element(By.Css_selector, "div")  # Wrong
element = driver.find_element(by.CSS_SELECTOR, "div")  # Wrong
element = driver.find_element(By.CSS_SELECTOR, "div")  # Right

Incorrect search criteria: It is crucial to provide the correct search criteria when using the By module’s methods. Failing to do so might result in NoSuchElementException or incorrect element identification.

If there are no issues in the import, capitalization, or installation of selenium, any error will likely be related to an incorrect search criteria.


This marks the end of the “How to import and use selenium.webdriver.common.by.By” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Leave a Comment