Selenium - Python - AttributeError: 'WebDriver'对象没有属性'find_element_by_name'

110

我正在尝试让Selenium与Chrome一起工作,但我不断遇到以下错误消息(以及类似的其他错误消息):

AttributeError:'WebDriver'对象没有属性'find_element_by_name'

对于find_element_by_id()find_element_by_class()等方法也会出现相同的问题。

我也无法调用send_keys()方法。

我只是在运行在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()

我使用的是 Google Chrome 版本 103.0.5060.53,从 Downloads 下载了 ChromeDriver 103.0.5060.53。
运行代码时,Chrome 打开并导航到 google.com,但收到以下输出:
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

注意:我替换了本帖中的文件路径。

我不认为DevTools的监听部分与该问题有关,但是我想包含它,以防万一。

4个回答

163

Selenium在版本4.3.0中删除了该方法。请参阅更改记录: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

您现在需要使用:

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

在你的例子中,它会变成:

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

search_box.send_keys('ChromeDriver')

search_box.submit()

为了提高可靠性,您应该考虑使用WebDriverWaitelement_to_be_clickable相结合。


6
这个方法很有效,谢谢!我希望有人能够更新ChromeDriver入门页面,包括这种新方法,这将节省很多麻烦。 - Alesss
3
请注意,如果你想要查找多个元素,你需要添加一个 "s",所以现在使用的是 find_elements() 而不是 find_elements_by_*。请参考这个非常好的SO答案:https://dev59.com/tVEG5IYBdhLWcg3wO3dP - MikhailRatner
同样适用于已弃用的 find_element_by_id(),它变成了 find_element("id", "some_id") - 太好了,谢谢。 - nyyrikki

53

34

find_element_by_* 和 find_elements_by_* 已经被弃用。你可以使用 find_element() 代替。

首先你需要导入:

from selenium.webdriver.common.by import By

然后,您可以使用它:

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

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

等等.. 查看find_element() API以获取所有用法


9

感谢michael-mintzfederikowsky提供线索,让我找到了问题的根源。

在我的情况下,我无法将代码与最新的Selenium版本兼容。因此,我采用了一个解决方法,将Selenium降级至4.2.0版本。

对于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
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接