101

I am trying to get Selenium working with Chrome, but I keep running into this error message (and others like it):

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

The same problem occurs with find_element_by_id(), find_element_by_class(), etc.

I also could not call send_keys().

I am just running the test code provided at ChromeDriver - WebDriver for Chrome - Getting started.

import time

from selenium import webdriver

driver = webdriver.Chrome("C:/Program Files/Chrome Driver/chromedriver.exe")  # Path to where I installed the web driver

driver.get('http://www.google.com/');

time.sleep(5) # Let the user actually see something!

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()

I am using Google Chrome version 103.0.5060.53 and downloaded ChromeDriver 103.0.5060.53 from Downloads.

When running the code, Chrome opens and navigates to google.com, but it receives the following output:

C:\Users\Admin\Programming Projects\Python Projects\Clock In\clock_in.py:21: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome("C:/Program Files/Chrome Driver/chromedriver.exe")  # Optional argument, if not specified will search path.

DevTools listening on ws://127.0.0.1:58397/devtools/browser/edee940d-61e0-4cc3-89e1-2aa08ab16432
[9556:21748:0627/083741.135:ERROR:device_event_log_impl.cc(214)] [08:37:41.131] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2)
[9556:21748:0627/083741.149:ERROR:device_event_log_impl.cc(214)] [08:37:41.148] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.156:ERROR:device_event_log_impl.cc(214)] [08:37:41.155] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.157:ERROR:device_event_log_impl.cc(214)] [08:37:41.156] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.157:ERROR:device_event_log_impl.cc(214)] [08:37:41.156] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Traceback (most recent call last):
  File "C:\[REDACTED]", line 27, in <module>
    search_box = driver.find_element_by_name('q')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'
[21324:19948:0627/083937.892:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled, ANGLE is

Note: I replaced the file path for this post.

I don't think that the DevTools listening section is related to the issue, but I thought I would include it, just in case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alesss
  • 1,013
  • 2
  • 4
  • 4

4 Answers4

138

Selenium just removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout

You now need to use:

driver.find_element("name", "q")

In your example, it would become:

search_box = driver.find_element("name", "q")

search_box.send_keys('ChromeDriver')

search_box.submit()

For improved reliability, you should consider using WebDriverWait in combination with element_to_be_clickable.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • 5
    This worked, thanks! I do wish that someone would update the ChromeDriver getting started page to include this new method, it would have saved a lot of headache. – Alesss Jun 27 '22 at 15:18
  • 3
    NOTE that if you want to find several elements you have to add an ```s```, so instead of ```find_elements_by_*``` it is now ```find_elements()```. See this very well done SO answer: https://stackoverflow.com/questions/69875125/find-element-by-commands-are-deprecated-in-selenium – MikhailRatner Jun 29 '22 at 15:45
  • works as well with deprecated `find_element_by_id()` which becomes `find_element("id", "some_id")` - great, thanks – nyyrikki May 10 '23 at 10:57
48

You need to import Selenium WebDriver using this code:

from selenium.webdriver.common.by import By

Next use this API:

Old API New API
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’)

and so on.

Source: Fixing Selenium AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_xpath’

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ann jenny
  • 581
  • 2
  • 3
31

find_element_by_* and find_elements_by_* are deprecated. You can use find_element() instead.

First you have to import:

from selenium.webdriver.common.by import By

Then you can use it with:

driver.find_element(By.XPATH, " ")
driver.find_elements(By.XPATH, " ")

driver.find_element(By.CLASS_NAME, " ")
driver.find_elements(By.CLASS_NAME, " ")

etc.. see find_element() API for all usage

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
federikowsky
  • 518
  • 2
  • 8
9

Thank you michael-mintz and federikowsky for giving the clue to the root cause.

In my case, I couldn't edit the code to be compatible with the latest Selenium version. So, as a workaround, I downgraded my Selenium to 4.2.0.

For Python 3:

  1. Make sure that your Selenium is 4.3.0 or higher. Start a Python interactive session and run:

    >>> import selenium
    >>> selenium.__version__
    
  2. Downgrade your Selenium

  • Either edit dependencies file (requirements.txt) and mention a specific version:

    selenium==4.2.0
    
  • Or, if you can't change dependencies file (requirements.txt), run these commands:

    pip3 install -r requirements.txt
    pip3 uninstall selenium
    pip3 install selenium==4.2.0
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Kuchin
  • 101
  • 5