[Fixed] Deprecationwarning: find_element_by_* commands are deprecated. please use find_element() instead

The find_element_by_() method will fail if you are using Selenium 4.0.0 or higher and trying to find the elements on the page using the find_element_by_() method. Deprecation There is a deprecation notice for the find_element_by_* functions. Rather than finding an element, please use find element().

In this tutorial, you will learn about DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead and how you can fix this error efficiently.

What is DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead?

The find_element_by_() is no longer supported in Selenium 4.0.0 and above, as stated in the changelog.
This is a deprecation warning for the function “find element by_*…”.

As an illustration of this problem, let us use a straightforward example.

#importing required Library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
 
def run(driver_path):    
    driver = webdriver.Chrome(executable_path=driver_path)    
    driver.get('https://google.com')    
    button = driver.find_element_by_class_name("code")
    button.click()

driver_path = "C:\Users\Coduber\Chrome.exe"
run(driver_path)

Output:

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

This DeprecationWarning was generated as a result of the changes made in response to the decision to simplify the APIs across all languages, and this accomplishes that goal.

Fix DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead?

The APIs have been simplified in the most recent version of Selenium, and the find_element_by_() and find_elements_by_() methods have been removed from the code.

[Fixed] Deprecationwarning: find_element_by_* commands are deprecated. please use find_element() instead

We can resolve the problem by making use of the new API methods that are available in Selenium 4.0.0 and later versions.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Old API Syntax
element = find_element_by_id("element_id")
elements = find_elements_by_id("element_id")
 
# New API Syntax
element = driver.find_element(By.ID, "element_id")
elements = driver.find_elements(By.ID, "element_id")

In Selenium, you can get an element by name.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Old API Syntax
element = find_element_by_name("element_name")
elements = find_elements_by_name("element_name")
 
# New API Syntax
element = driver.find_element(By.NAME, "element_name")
elements = driver.find_elements(By.NAME, "element_name")

Selenium Retrieves an element based on its link text.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Syntax As per Old API
element = find_element_by_link_text("element_link_text")
elements = find_elements_by_link_text("element_link_text")
 
# Syntax As per New API
element = driver.find_element(By.LINK_TEXT, "element_link_text")
elements = driver.find_elements(By.LINK_TEXT, "element_link_text")

Selenium Retrieves an element from a partial link text.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Syntax As per Old API
element = find_element_by_partial_link_text("element_partial_link_text")
elements = find_elements_by_partial_link_text("element_partial_link_text")
 
# Syntax As per New API
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "element_partial_link_text")

Selenium Get Element By Tag Name.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Syntax As per Old API
element = find_element_by_tag_name("element_tag_name")
elements = find_elements_by_tag_name("element_tag_name")
 
# Syntax As per New API
element = driver.find_element(By.TAG_NAME, "element_tag_name")
elements = driver.find_elements(By.TAG_NAME, "element_tag_name")

Using Selenium Get Element By CSS Selector.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Syntax As per Old API
element = find_element_by_css_selector("element_css_selector")
elements = find_elements_by_css_selector("element_css_selector")
 
# Syntax As per New API
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
elements = driver.find_elements(By.CSS_SELECTOR, "element_css_selector")

Selenium Get Element By XPath is a tool for testing web applications.

#Importing Required Library
from selenium.webdriver.common.by import By
 
# Syntax As per Old API
element = find_element_by_xpath("element_xpath")
elements = find_elements_by_xpath("element_xpath")
 
# Syntax As per New API
element = driver.find_element(By.XPATH, "element_xpath")
elements = driver.find_elements(By.XPATH, "element_xpath")

Wrap Up

The find_element_by_* and DeprecationWarning: find element by_* commands are no longer supported. If possible, please use find element() instead, which is more common in Selenium 4.0.0 and higher because find_element_by_() and find_elements_by_() have been deprecated since Selenium 4.0.0. All of the changes were made as a result of the decision to streamline the APIs across all of the languages.

As shown in the examples, we can resolve the problem by utilizing the new API method syntaxes find element() and find elements() to locate the problematic elements.

Further Read:

  1. [Fixed] DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  2. Python Random Module – Generate Random Numbers/Sequences
  3. Pandas Update Row Value In Python DataFrame
  4. [Fixed] Python TypeError: unhashable type: ‘list’

Leave a Comment