AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. 'WebDriver' object has no attribute 'find_element_by_id'
  2. 'WebDriver' object has no attribute 'find_element_by_name'
  3. 'WebDriver' object has no attribute 'find_element_by_xpath'

# AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

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.

attributeerror webdriver object has no attribute find element by id

As the changelog of Selenium states:

  • Deprecated find_element_by_* and find_elements_by_* are now removed
  • Deprecated Opera support has been removed
The find_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.

main.py
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:

  • find_element(By.ID, "id")
  • find_element(By.NAME, "name")

Here are some examples from the Selenium docs.

main.py
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.

# Downgrade your Selenium version to 4.2.0

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.

main.py
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.

requirements.txt
selenium==4.2.0

You can use the pip show selenium command to check which version of the package is installed.

shell
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.

main.py
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.

# AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'

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.

attributeerror webdriver object has no attribute find element by name

As the changelog of Selenium states:

  • Deprecated find_element_by_* and find_elements_by_* are now removed
  • Deprecated Opera support has been removed
The find_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.

main.py
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:

  • find_element(By.ID, "id")
  • find_element(By.NAME, "name")

Here are some examples from the Selenium docs.

main.py
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.

# Downgrade your Selenium version to 4.2.0

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.

main.py
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.

requirements.txt
selenium==4.2.0

You can use the pip show selenium command to check which version of the package is installed.

shell
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.

main.py
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.

# AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

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.

attributeerror webdriver object has no attribute find element by xpath

As the changelog of Selenium states:

  • Deprecated find_element_by_* and find_elements_by_* are now removed
  • Deprecated Opera support has been removed
The find_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.

main.py
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:

  • find_element(By.ID, "id")
  • find_element(By.NAME, "name")
  • find_element(By.XPATH, "xpath")

Here are some examples from the Selenium docs.

main.py
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.

# Downgrade your Selenium version to 4.2.0

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.

main.py
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.

requirements.txt
selenium==4.2.0

You can use the pip show selenium command to check which version of the package is installed.

shell
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev