我试图让Selenium与Chrome一起工作,但我一直遇到这个错误消息(以及其他类似的消息):
属性错误:“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条答案
按热度按时间qxgroojn1#
Selenium在
4.3.0
版本中删除了这个方法。看看变化:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES您现在需要用途:
在您的示例中,它将变为:
为了提高可靠性,您应该考虑将
WebDriverWait
与element_to_be_clickable
结合使用。kwvwclae2#
您需要使用以下代码导入Selenium WebDriver:
接下来使用这个API:
| 旧API|新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')|
等
来源:* Fixing Selenium AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_xpath’ *
but5z9lq3#
find_element_by_* 和find_elements_by_* 已弃用。可以使用 find_element() 代替。
首先,你需要导入:
然后你可以使用它与:
等等。查看find_element()API了解所有用法
u0sqgete4#
感谢michael-mintz和federikowsky给出了根本原因的线索。
在我的例子中,我无法编辑代码以与最新的Selenium版本兼容。因此,作为一种变通方法,我将Selenium降级到4.2.0。
对于Python 3:
1.请确保您的Selenium是4.3.0或更高版本。启动Python交互式会话并运行:
1.降级您的Selenium