Skip to content

Selenium WebDriver - Fixing AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'

Problem

Many Selenium users encounter the error AttributeError: 'WebDriver' object has no attribute 'find_element_by_name' when trying to locate elements on web pages. This error occurs with all the deprecated find_element_by_* methods including:

  • find_element_by_id()
  • find_element_by_class_name()
  • find_element_by_xpath()
  • And related find_elements_by_* methods

Additionally, users may encounter deprecation warnings about the executable_path parameter when initializing the WebDriver.

Root Cause

In Selenium version 4.3.0 (released in June 2022), the Selenium development team completely removed the deprecated find_element_by_* and find_elements_by_* methods from the Python bindings. These methods were marked as deprecated in earlier versions but were fully removed in 4.3.0 to encourage adoption of the newer, more flexible approach.

WARNING

The Selenium team announced this change in their release notes, stating they've moved to a more unified approach for element location.

Solution

1. Update Your Import Statements

To use the modern Selenium API, you must import the By class:

python
from selenium.webdriver.common.by import By

2. Use the New find_element() Method

Replace all deprecated methods with the new standardized approach:

Old MethodNew Method
find_element_by_id('id')find_element(By.ID, 'id')
find_element_by_name('name')find_element(By.NAME, 'name')
find_element_by_xpath('xpath')find_element(By.XPATH, 'xpath')
find_element_by_class_name('class')find_element(By.CLASS_NAME, 'class')
find_element_by_css_selector('css')find_element(By.CSS_SELECTOR, 'css')
find_element_by_link_text('text')find_element(By.LINK_TEXT, 'text')

Complete Fixed Example

Here's how to convert the original Google search example:

python
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# Modern driver initialization (no more executable_path)
driver = webdriver.Chrome()

driver.get('http://www.google.com/')
time.sleep(5)  # Let the user actually see something!

# Using the new find_element method with By.NAME
search_box = driver.find_element(By.NAME, 'q')

search_box.send_keys('ChromeDriver')
search_box.submit()

time.sleep(5)  # Let the user actually see something!
driver.quit()
python
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
python
from selenium.webdriver.common.by import By

search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys('ChromeDriver')
search_box.submit()

3. Update WebDriver Initialization

The warning about executable_path deprecation can be resolved by using the Service object:

python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Modern WebDriver initialization
service = Service('C:/Program Files/Chrome Driver/chromedriver.exe')
driver = webdriver.Chrome(service=service)

Instead of using time.sleep(), which is not reliable for web automation, use explicit waits:

python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Wait for element to be present and clickable
wait = WebDriverWait(driver, 10)
search_box = wait.until(EC.element_to_be_clickable((By.NAME, 'q')))

search_box.send_keys('ChromeDriver')
search_box.submit()

Alternative Solution: Downgrade Selenium

If you cannot update your code immediately, you can temporarily downgrade to Selenium 4.2.0:

bash
pip uninstall selenium
pip install selenium==4.2.0

DANGER

Downgrading is not recommended as a long-term solution. Future Selenium updates may introduce security fixes and performance improvements that won't be available in older versions.

Conclusion

The AttributeError: 'WebDriver' object has no attribute 'find_element_by_name' occurs because Selenium 4.3.0+ removed the deprecated element location methods. The solution is to:

  1. Import from selenium.webdriver.common.by import By
  2. Replace find_element_by_* with find_element(By.*, 'value')
  3. Update WebDriver initialization to use Service objects
  4. Consider implementing explicit waits instead of time.sleep()

This modern approach provides better code maintainability and follows Selenium's current best practices.