Last updated: Apr 10, 2024
Reading timeยท6 min
The Selenium "AttributeError: 'WebDriver' object has no attribute
'find_element_by_id'" occurs because the find_element_by_id
method has been
deprecated and removed starting Selenium 4.3.0.
Use the find_element
method instead or downgrade your selenium version to
4.2.0.
As the changelog of Selenium states:
find_element_by_*
and find_elements_by_*
are now removedfind_element_by_*
methods have been replaced by find_element
and the find_elements_by_*
methods have been replaced by find_elements
.Here is an example of how to use the find_element
method instead of
find_element_by_id
.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.python.org/") time.sleep(2) # ๐๏ธ using find_element method ๐๏ธ search_field = driver.find_element(By.ID, 'id-search-field') search_field.send_keys('for loop') search_field.submit() time.sleep(2) driver.close()
We use the By
class to specify which attribute is used to locate elements on
the page.
The find_element_by_*
methods have now become:
Here are some examples from the Selenium docs.
from selenium.webdriver.common.by import By # โ Locate a single element (New API) find_element(By.ID, "id") find_element(By.NAME, "name") find_element(By.XPATH, "xpath") find_element(By.LINK_TEXT, "link text") find_element(By.PARTIAL_LINK_TEXT, "partial link text") find_element(By.TAG_NAME, "tag name") find_element(By.CLASS_NAME, "class name") find_element(By.CSS_SELECTOR, "css selector") # โ Locate multiple elements (New API) find_elements(By.XPATH, '//button')
If you need to look at more examples of using the new Selenium API, check out this section of the docs.
If you aren't able to update your code, you can downgrade your Selenium version to 4.2.0 to use the old API.
To solve the error, downgrade your
Selenium version to 4.2.0, which is the
last version that supports the find_element_by_id()
method.
Open your terminal in your project's root directory and run the following command.
pip install selenium==4.2.0 --force-reinstall pip3 install selenium==4.2.0 --force-reinstall # ๐๏ธ if you don't have pip in PATH environment variable python -m pip install selenium==4.2.0 --force-reinstall python3 -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ py alias (Windows) py -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ For Jupyter Notebook !pip install selenium==4.2.0 --force-reinstall
If you have a requirements.txt file, you can add the following line.
selenium==4.2.0
You can use the pip show selenium
command to check which version of the
package is installed.
pip show selenium pip3 show selenium python -m pip show selenium python3 -m pip show selenium
Once you downgrade your Selenium version to 4.2.0, you will be able to use the
deprecated find_element_by_id
API.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.python.org/") time.sleep(2) # ๐๏ธ using find_element_by_id (Selenium version < 4.3.0) search_field = driver.find_element_by_id('id-search-field') search_field.send_keys('for loop') search_field.submit() time.sleep(2) driver.close()
Downgrading your Selenium version to 4.2 might not be a long-term solution, but it gets the job done if you aren't able to update your code right away.
The Selenium "AttributeError: 'WebDriver' object has no attribute
'find_element_by_name'" occurs because the find_element_by_name
method has
been deprecated and removed starting Selenium 4.3.0.
Use the find_element
method instead or downgrade your selenium version to
4.2.0.
As the changelog of Selenium states:
find_element_by_*
and find_elements_by_*
are now removedfind_element_by_*
methods have been replaced by find_element
and the find_elements_by_*
methods have been replaced by find_elements
.Here is an example of how to use the find_element
method instead of
find_element_by_name
.
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com") time.sleep(4) # ๐๏ธ using find_element method ๐๏ธ search_field = driver.find_element(By.NAME, 'q') search_field.send_keys('bobbyhadz.com') search_field.submit() time.sleep(2) driver.close()
We use the By
class to specify which attribute is used to locate elements on
the page.
The find_element_by_*
methods have now become:
Here are some examples from the Selenium docs.
from selenium.webdriver.common.by import By # โ Locate a single element (New API) find_element(By.ID, "id") find_element(By.NAME, "name") find_element(By.XPATH, "xpath") find_element(By.LINK_TEXT, "link text") find_element(By.PARTIAL_LINK_TEXT, "partial link text") find_element(By.TAG_NAME, "tag name") find_element(By.CLASS_NAME, "class name") find_element(By.CSS_SELECTOR, "css selector") # โ Locate multiple elements (New API) find_elements(By.XPATH, '//button')
If you need to look at more examples of using the new Selenium API, check out this section of the docs.
If you aren't able to update your code, you can downgrade your Selenium version to 4.2.0 to use the old API.
To solve the error, downgrade your Selenium version to 4.2.0, which is the last
version that supports the find_element_by_name
method.
Open your terminal in your project's root directory and run the installation command with the --force-reinstall option.
pip install selenium==4.2.0 --force-reinstall pip3 install selenium==4.2.0 --force-reinstall # ๐๏ธ if you don't have pip in PATH environment variable python -m pip install selenium==4.2.0 --force-reinstall python3 -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ py alias (Windows) py -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ For Jupyter Notebook !pip install selenium==4.2.0 --force-reinstall
If you have a requirements.txt
file, you can add the following line.
selenium==4.2.0
You can use the pip show selenium
command to check which version of the
package is installed.
pip show selenium pip3 show selenium python -m pip show selenium python3 -m pip show selenium
Once you downgrade your Selenium version to 4.2.0, you will be able to use the
deprecated find_element_by_name
API.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com") time.sleep(4) # ๐๏ธ using find_element_by_name (Selenium version < 4.3.0) search_field = driver.find_element_by_name('q') search_field.send_keys('bobbyhadz.com') search_field.submit() time.sleep(2) driver.close()
Downgrading your Selenium version to 4.2 might not be a long-term solution, but it gets the job done if you aren't able to update your code right away.
The Selenium "AttributeError: 'WebDriver' object has no attribute
'find_element_by_xpath'" occurs because the find_element_by_xpath
method has
been deprecated and removed starting Selenium 4.3.0.
Use the find_element
method instead or downgrade your Selenium version to
4.2.0.
As the changelog of Selenium states:
find_element_by_*
and find_elements_by_*
are now removedfind_element_by_*
methods have been replaced by find_element
and the find_elements_by_*
methods have been replaced by find_elements
.Here is an example of how to use the find_element
method instead of
find_element_by_xpath
.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com") time.sleep(3) # ๐๏ธ Using find_element() method ๐๏ธ search_field = driver.find_element( By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input' ) search_field.send_keys('bobbyhadz.com') search_field.submit() time.sleep(2) driver.close()
We use the By
class to specify which attribute is used to locate elements on
the page.
The find_element_by_*
methods have now become:
Here are some examples from the Selenium docs.
from selenium.webdriver.common.by import By # โ Locate a single element (New API) find_element(By.ID, "id") find_element(By.NAME, "name") find_element(By.XPATH, "xpath") find_element(By.LINK_TEXT, "link text") find_element(By.PARTIAL_LINK_TEXT, "partial link text") find_element(By.TAG_NAME, "tag name") find_element(By.CLASS_NAME, "class name") find_element(By.CSS_SELECTOR, "css selector") # โ Locate multiple elements (New API) find_elements(By.XPATH, '//button')
If you need to look at more examples of using the new Selenium API, check out this section of the docs.
If you aren't able to update your code, you can downgrade your Selenium version to 4.2.0 to use the old API.
To solve the error, downgrade your Selenium version to 4.2.0, which is the last
version that supports the find_element_by_xpath
method.
Open your terminal in your project's root directory and run the following command.
pip install selenium==4.2.0 --force-reinstall pip3 install selenium==4.2.0 --force-reinstall # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install selenium==4.2.0 --force-reinstall python3 -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ py alias (Windows) py -m pip install selenium==4.2.0 --force-reinstall # ๐๏ธ For Jupyter Notebook !pip install selenium==4.2.0 --force-reinstall
If you have a requirements.txt
file, you can add the following line.
selenium==4.2.0
You can use the pip show selenium
command to check which version of the
package is installed.
pip show selenium pip3 show selenium python -m pip show selenium python3 -m pip show selenium
Once you downgrade your Selenium version to 4.2.0, you will be able to use the
deprecated find_element_by_xpath
API.
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com") time.sleep(3) # ๐๏ธ Using find_element_by_xpath (Selenium < 4.3.0) ๐๏ธ search_field = driver.find_element_by_xpath( '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input' ) search_field.send_keys('bobbyhadz.com') search_field.submit() time.sleep(2) driver.close()
Downgrading your Selenium version to 4.2 might not be a long-term solution, but it gets the job done if you aren't able to update your code right away.