We Are Going To Discuss About find_element_by_* commands are deprecated in selenium. So lets Start this Python Article.
find_element_by_* commands are deprecated in selenium
- How to solve find_element_by_* commands are deprecated in selenium
Instead you have to use
find_element()
. As an example:
You have to include the following importsfrom selenium.webdriver.common.by import By
Usingclass_name
:button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:button = driver.find_element(By.CLASS_NAME, "quiz_button")
In similar lines you also have to change the following:
Usingid
:element = find_element_by_id("element_id")
Needs be replaced with:element = driver.find_element(By.ID, "element_id")
Usingname
:element = find_element_by_name("element_name")
Needs be replaced with:element = driver.find_element(By.NAME, "element_name")
Usinglink_text
:element = find_element_by_link_text("element_link_text")
Needs be replaced with:element = driver.find_element(By.LINK_TEXT, "element_link_text")
Usingpartial_link_text
:element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Usingtag_name
:element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:element = driver.find_element(By.TAG_NAME, "element_tag_name")
Usingcss_selector
:element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Usingxpath
:element = find_element_by_xpath("element_xpath")
Needs be replaced with:element = driver.find_element(By.XPATH, "element_xpath")
Note: If you are searching and replacing to implement the above changes, you will need to do the same thing forfind_elements_*
, i.e. the plural forms offind_element_*
.
You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/ - find_element_by_* commands are deprecated in selenium
Instead you have to use
find_element()
. As an example:
You have to include the following importsfrom selenium.webdriver.common.by import By
Usingclass_name
:button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:button = driver.find_element(By.CLASS_NAME, "quiz_button")
In similar lines you also have to change the following:
Usingid
:element = find_element_by_id("element_id")
Needs be replaced with:element = driver.find_element(By.ID, "element_id")
Usingname
:element = find_element_by_name("element_name")
Needs be replaced with:element = driver.find_element(By.NAME, "element_name")
Usinglink_text
:element = find_element_by_link_text("element_link_text")
Needs be replaced with:element = driver.find_element(By.LINK_TEXT, "element_link_text")
Usingpartial_link_text
:element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Usingtag_name
:element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:element = driver.find_element(By.TAG_NAME, "element_tag_name")
Usingcss_selector
:element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Usingxpath
:element = find_element_by_xpath("element_xpath")
Needs be replaced with:element = driver.find_element(By.XPATH, "element_xpath")
Note: If you are searching and replacing to implement the above changes, you will need to do the same thing forfind_elements_*
, i.e. the plural forms offind_element_*
.
You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/
Solution 1
This error message…
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
…implies that the find_element_by_*
commands are deprecated in the latest Selenium Python libraries.
As @AutomatedTester mentions: 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.
Solution
Instead you have to use find_element()
. As an example:
You have to include the following imports
from selenium.webdriver.common.by import By
-
Using
class_name
:button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:
button = driver.find_element(By.CLASS_NAME, "quiz_button")
In similar lines you also have to change the following:
-
Using
id
:element = find_element_by_id("element_id")
Needs be replaced with:
element = driver.find_element(By.ID, "element_id")
-
Using
name
:element = find_element_by_name("element_name")
Needs be replaced with:
element = driver.find_element(By.NAME, "element_name")
-
Using
link_text
:element = find_element_by_link_text("element_link_text")
Needs be replaced with:
element = driver.find_element(By.LINK_TEXT, "element_link_text")
-
Using
partial_link_text
:element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
-
Using
tag_name
:element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:
element = driver.find_element(By.TAG_NAME, "element_tag_name")
-
Using
css_selector
:element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
-
Using
xpath
:element = find_element_by_xpath("element_xpath")
Needs be replaced with:
element = driver.find_element(By.XPATH, "element_xpath")
Note: If you are searching and replacing to implement the above changes, you will need to do the same thing for find_elements_*
, i.e. the plural forms of find_element_*
.
You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/
Original Author undetected Selenium Of This Content
Solution 2
@DebanjanB mentioned and explained the new structure , also it’s better use these lines:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
s = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=s)
Original Author Mori Of This Content
Solution 3
As others mentioned, you should use find_element()
or find_elements()
instead of find_element_by_*()
or find_elements_by_*()
.
I wrote the regex pattern to replace the deprecated methods to new one, so try this if you need.
# from - e.g. find_element_by_id("test")
find_element(s?)_by_([a-z]+)\((.*)
# to - e.g. find_element(By.ID, "test")
find_element$1(By.\U$2\E, $3
Note: you need the import line to use the new methods
from selenium.webdriver.common.by import By
Original Author Lagyu Of This Content
Solution 4
Here is an example which will clarify things a bit:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
chrome_driver_path = "C:\Development\chromedriver_win32\chromedriver.exe"
s = Service(chrome_driver_path)
driver = webdriver.Chrome(service=s)
driver.get('https://www.amazon.in/Apple-MacBook-Chip-13-inch-256GB/dp/B08N5VSQNG/')
title = driver.find_element(by=By.ID, value="productTitle") # by=By.ID, By.TAG_NAME, By.CLASS_NAME, By.NAME
print(title.text)
# driver.close() # Closes the tab which was opened earlier
driver.quit() # Quits the entire browser.
Original Author rohanmech Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.