In this post, we are going to learn how to Solve selenium find_element_by_* commands are deprecated, and Please use find_element() instead. To solve this warning we have to import "from selenium.webdriver.common.by import By"
our code and need to use its different methods to solve selenium find_element_by to find elements by different attributes. Let us first understand how this warning arises. When we run the below code, We will see the DeprecationWarning on the console output
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://google.com")
print('driver Title:',driver.title)
button = driver.find_elements_by_name("q")
driver.quit()
Output
DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
button = driver.find_elements_by_name("q")
1. Selenium Find element by ID
find_element_by_* commands are deprecated to resolve it while using find_element_by_id
we have to use find_element by using the following steps
- For this, First, need to import
"from selenium.webdriver.common.by
import By
"
- Replace
find_element_by_id("element_id")
withdriver
.
find_element(By.ID
,"element_id")
for a single element. - For Multiple elements Replace find_elements_by_id(“element_id”) with Mutielement = driver.find_elements(By.ID, “element_id”).
from selenium.webdriver.common.by import By
# old syntax
element = driver.find_element_by_id("element_id")
Mutielement = driver.find_elements_by_id("element_id")
# New Syntax
element = driver.find_element(By.ID, "element_id")
Mutielement = driver.find_elements(By.ID, "element_id")
2. Selenium Find element by classname
Find_element_by_* commands are deprecated to resolve it while using find_element_by_id
we have to use find_element by using the following steps.
- For this, First, need to import
"from selenium.webdriver.common.by
import By
"
- Replace
find_element_by_class_name("class-name")
withdriver
.
find_element(By.CLASS_NAME
,"class-name")
for a single element. - For Multiple elements Replace driver.find_elements(By.CLASS_NAME, “class-name”) with mutiElems= driver.find_elements(By.CLASS_NAME,”class-name”).
from selenium.webdriver.common.by import By
element= driver.find_element_by_class_name("class-name")
mutiElems= driver.find_elements_by_class_name("class-name")
# replaced with new syntax
element= driver.find_element(By.CLASS_NAME, "class-name")
mutiElems= driver.find_elements(By.CLASS_NAME,"class-name")
3. Selenium Find element by name
- For this, First, need to import
"from selenium.webdriver.common.by
import By
"
- Replace
find_element_by_name("element_namee")
withdriver
.
find_element(By.element_name
,"element_name")
for a single element. - For Multiple elements Replace driver.find_elements(By.CLASS_NAME, “class-name”) with new syntax
Elename = driver.find_element_by_name("element_name")
Elenames = driver.find_elements_by_name("element_name")
# replaced with new syntax
Elename = driver.find_element(By.NAME, "element_name")
MutiElenames = driver.find_elements(By.NAME,"element_name")
4. Selenium Find elements by Using link text
- For this, First, need to import
"from selenium.webdriver.common.by
import By
"
- Replace driver.
find_element_by_link_text("element_namee")
withdriver.find_element(By.LINK_TEXT, "element_link_text")
for a single element. - Repaeat same step for Multiple elements Replace driver.find_elements_by_link_text(“element_link_text”) with new syntax .
#using old syntax
element = driver.find_element_by_link_text("element_link_text")
mutielement = driver.find_elements_by_link_text("element_link_text")
#using new syntax
element = driver.find_element(By.LINK_TEXT, "element_link_text")
mutielement = driver.find_elements(By.LINK_TEXT,"element_link_text")
5. Selenium Find elements by Partial link text
In this Python program example, to resolve this error find_element_by_* commands are deprecated to resolve it while using find_element_by_id
we have to use find_element by using the following steps.
- For this, First, need to import
"from selenium.webdriver.common.by import By"
Replace old syntax with new and the error will be
resolved
element = driver.find_element_by_partial_link_text("element_partial_link_text")
Mutielem = driver.find_elements_by_partial_link_text("element_partial_link_text")
#new syntax to replace with
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Mutielem = driver.find_elements(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
6. Selenium Find element by Tag name
When we use find_element_by_tag_name in Selenium 4.0.0 the Find_element_by_* commands error is raised.To resolve First of all “import from selenium.webdriver.common.by import By” and replace old code with new code as mentioned below for single or multiple elements.
from selenium.webdriver.common.by import By
#old syntax
element = driver.find_element_by_tag_name("element_tag_name")
Mutielem = driver.find_elements_by_tag_name("element_tag_name")
#new syntax to replace with
element = driver.find_element(By.TAG_NAME, "element_tag_name")
Mutielem = driver.find_elements(By.TAG_NAME, "element_tag_name")
7. Selenium Find element by Xpath
When we find an element by find_element_by_xpath in Selenium 4.0.0 the Find_element_by_* commands error is raised. To resolve First of all "import from selenium.webdriver.common.by import By"
and replace it with new code as mentioned below with new old for single or multiple elements.
from selenium.webdriver.common.by import By
#old syntax
element = driver.find_element_by_xpath("element_xpath")
Mutielem = driver. find_elements_by_xpath("element_xpath")
#new syntax to replace with
element = driver.find_element(By.XPATH, "element_xpath")
Mutielem = driver.find_elements(By.XPATH, "element_xpath")
8. Selenium Find element by CSS selector
When we find an element by find_element_by_css_selector in Selenium 4.0.0 the Find_element_by_* commands error is raised. To resolve First of all "import from selenium.webdriver.common.by import By"
and replace old code with new code as mentioned below for single or multiple elements as per need
from selenium.webdriver.common.by import By
#old syntax
element = driver.find_element_by_css_selector("element_css_selector")
Mutielem =driver. find_elements_by_css_selector("element_css_selector")
#new syntax to replace with
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Mutielem = driver.find_elements(By.CSS_SELECTOR, "element_css_selector")