[python] webdriver.Chrome报错

报错信息

Traceback (most recent call last):
  File "C:\Users\31530\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
  File "C:\Users\31530\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\coding\python\py_test.py", line 4, in <module>
    driver = webdriver.Chrome("D:\webDriver\chromedriver-win64\chromedriver.exe")
  File "C:\Users\31530\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\Users\31530\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "C:\Users\31530\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
AttributeError: 'str' object has no attribute 'capabilities'

原因与解决办法

原本按照书上教的写的是

driver = webdriver.Chrome("D:\my\path\to\driver\chromeDriver.exe")

后来查了之后发现新版本的Selenium已经不用这样写了
直接改成下面即可,同理其他浏览器也是一样, 不需要加driver路径

driver = webdriver.Chrome()

问题2

修改后可以唤起chrome浏览器了, 但是还是报错

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

解决办法

#添加
from selenium.webdriver.common.by import By

#修改find_element_by_name为find_element(By.NAME,"q") 
elem = driver.find_element(By.NAME,"q") 

更多的新版Selenium可以参考官方文档

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容