[Fix] DeprecationWarning find_element_by_* commands are deprecated. Please use find_element() instead

The error “Unable to negotiate with 40.74.28.9 port 22: no matching host key type found. Their offer: ssh-rsa” is an error that is invoked whenever you are coding in python using selenium. Given below is the snippet of the error, which you can find here:


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

I would like to share with you the steps I took to fix the “DeprecationWarning find_element_by_* commands are deprecated. Please use find_element() instead” in your file:

Why “DeprecationWarning find_element_by_* commands are deprecated. Please use find_element() instead” error is seen?

The error, “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead” is seen because; the find_element_by_*  class, and its commands have been deprecated in the latest update of Selenium Python libraries. The DeprecationWarning were due to the changes made because of the decision to simplify the APIs across different languages, and this does that.

The detailed solution to fix the error “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”, is given below.

How to fix the error, “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”?

For a simple fix of the error, you can try to replace the methods which are outdated and not being currently used with methods which are present on the in the latest update of Selenium Python libraries.

To fix the error, “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”, follow one of the methods given below for any particular function which may cause the error:

Here is the list of all the replacements you will have to make:

First, you will have to include find_element()

You will have to import the below mentioned code at the start:

from selenium.webdriver.common.by import By

The following classes will have to be replaced to solve the error.

Replacement 1: Instead of Using class_name:

button = driver.find_element_by_class_name("quiz_button") 

it has to be replaced with:

button = driver.find_element(By.CLASS_NAME, "quiz_button")

Replacement 2: Instead of Using id:

element = find_element_by_id("element_id") 

it has to be replaced with:

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

Replacement 3: Instead of Using name:

element = find_element_by_name("element_name") 

it has to be replaced with:

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

Replacement 4: Instead of Using link_text:

element = find_element_by_link_text("element_link_text")

it has to be replaced with:

element = driver.find_element(By.LINK_TEXT, "element_link_text")

Replacement 5: Instead of Using partial_link_text:

element = find_element_by_partial_link_text("element_partial_link_text") 

it has to be replaced with:

element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")

Replacement 6: Instead of Using tag_name:

element = find_element_by_tag_name("element_tag_name") 

it has to be replaced with:

element = driver.find_element(By.TAG_NAME, "element_tag_name")

Replacement 7: Instead of Using css_selector:

element = find_element_by_css_selector("element_css_selector") 

it has to be replaced with:

element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")

Replacement 8: Instead of Using xpath:

element = find_element_by_xpath("element_xpath") 

it has to be replaced with:

element = driver.find_element(By.XPATH, "element_xpath")

This should fix the error “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”.

Conclusion

To fix the error “DeprecationWarning find_element_by_* commands are deprecated. Please use find_element() instead”, you can try to replace the methods which are outdated and not being currently used with methods which are present on the in the latest update of Selenium Python libraries.