[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.

1 comment
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like