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

If you use Selenium 4.0.0 or above and try to find the elements on the page using the find_element_by_*() method, you will get an error saying DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

In this article, we will look at what exactly is DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead means and how to resolve this error with examples.

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

In Selenium 4.0.0 and above, find_element_by_* commands are deprecated, which is mentioned in the changelog.

Specify that the "find_element_by_* ..." warning is a deprecation warning (#9700)

Let us take a simple example to demonstrate this issue.

def run(driver_path):    
    driver = webdriver.Chrome(executable_path=driver_path)    
    driver.get('https://itsmycode.com')    
    button = driver.find_element_by_class_name("code")
    button.click()
run(driver_path)

Output

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

AutomatedTester mentions the reason for this change in GitHub issues “This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages, and this does that.”

How to fix DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead?

The APIs are simplified in the latest version of Selenium, and both find_element_by_*() and find_elements_by_*() methods.

We can fix the issue by using the new API methods that are available in Selenium 4.0.0 onwards.

Selenium Get Element By ID

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")

Selenium Get Element By Name

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 Get Element By Link Text

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_link_text("element_link_text")
elements = find_elements_by_link_text("element_link_text")

# New API Syntax
element = driver.find_element(By.LINK_TEXT, "element_link_text")
elements = driver.find_elements(By.LINK_TEXT, "element_link_text")

Selenium Get Element By Partial Link Text

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_partial_link_text("element_partial_link_text")
elements = find_elements_by_partial_link_text("element_partial_link_text")


# New API Syntax
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

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_tag_name("element_tag_name")
elements = find_elements_by_tag_name("element_tag_name")

# New API Syntax
element = driver.find_element(By.TAG_NAME, "element_tag_name")
elements = driver.find_elements(By.TAG_NAME, "element_tag_name")

Selenium Get Element By Css Selector

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_css_selector("element_css_selector")
elements = find_elements_by_css_selector("element_css_selector")

# New API Syntax
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

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_xpath("element_xpath")
elements = find_elements_by_xpath("element_xpath")

# New API Syntax
element = driver.find_element(By.XPATH, "element_xpath")
elements = driver.find_elements(By.XPATH, "element_xpath")

Conclusion

The DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead mainly occurs in Selenium 4.0.0 and above as find_element_by_*() and find_elements_by_*() are deprecated. The changes made with respect to the decision to simplify the APIs across the languages.

We can resolve the issue by using the new API method syntax find_element() and find_elements() methods as shown in the examples.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Rjust().Png

Python String rjust()

Table of Contents Hide rjust() Syntaxrjust() Parametersrjust() Return valueExample 1 : Python program to right justify a stringExample 2: rjust() Method With * fillcharExample 3: Returns original string if the…
View Post
Python Bin()

Python bin()

Table of Contents Hide bin() Syntaxbin() Parametersbin() Return ValueExample 1: Convert integer to binary using bin() methodExample 2: TypeError: ‘float’ object cannot be interpreted as an integerExample 3: Convert an…
View Post
Python String Isidentifier()

Python String isidentifier()

Table of Contents Hide isidentifier() Syntaxisidentifier() Parametersisidentifier() Return ValueExample 1: How isidentifier() works? Example 1: How to use isidentifier() method? The Python String isidentifier() method is a built-in function that…
View Post