. Advertisement .
..3..
. Advertisement .
..4..
When you try to do anything automated in this tool, the following error message appears: “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead“.
Don’t worry too much. This article will help all readers resolve this error with this well-known program.
When would the error “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead” happen?
You are using selenium webdriver in my Python project, and when you use find element by class name, you get the following error.
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
Or when you start the function like this.
def run(driver_path): driver = webdriver.Chrome(executable_path=driver_path) driver.get('https://tproger.ru/quiz/real-programmer/') button = driver.find_element_by_class_name("quiz_button") button.click() run(driver_path)
And you face the above error like this.
<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(executable_path=driver_path) <ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead button = driver.find_element_by_class_name("quiz_button")
You have got the error ”DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead” because according to the changelog, find_element_by_* is no longer supported in Selenium 4.0.0 and beyond. Moreover, the DeprecationWarning reflects the done changes which relate to the decisions to simplify the APIs across the languages, and this executes that.
The top techniques to solve “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”
To resolve this fundamental issue, you can use the syntax described below.
Method 1: Use find_element()
The find element by_* commands have been deprecated in the most recent Selenium Python libraries. As a result, you must use find_element() to solve “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead” error exactly as shown.
from selenium.webdriver.common.by import By
fnd_btn = driver.find_element(By.CLASS_NAME, "your_class_name")
Method 2: Use these codes
The way we provided below is not a bad solution for you. Let’s do it to solve your issue effectively.
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("chromedriver.exe")
# driver.find_element_by_name("NAME")
driver.find_element(By.NAME, "NAME")
# driver.find_element_by_class_name("CLASS_NAME")
driver.find_element(By.CLASS_NAME, "CLASS_NAME")
# driver.find_element_by_id("ID")
driver.find_element(By.ID, "ID")
# driver.find_element_by_xpath("XPATH")
driver.find_element(By.XPATH, "XPATH")
Method 3: Use these codes
You can do as the following to fix the error and get your desired result.
button = driver.find_element(By.CLASS_NAME, "quiz_button")
element = driver.find_element(By.ID, "element_id")
element = driver.find_element(By.NAME, "element_name")
element = driver.find_element(By.LINK_TEXT, "element_link_text")
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
element = driver.find_element(By.TAG_NAME, "element_tag_name")
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Method 4: Use the new API
Excepting the above methods, using the new API that is accessible in the 4.0.0 version of Selenium 4.0.0 and laters version is a great way to solve the error “deprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead”. Let’s look at these following instances to understand more about this method.
In Selenium you can get element by many ways, such as:
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")
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")
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")
By a part of 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")
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")
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")
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
In short, the four approaches presented above are simple solutions to the aforementioned major error: “DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead“. Please leave your questions in the comment so that our team can check them. I wish you all a fruitful day with our Python tool.
Leave a comment