报错信息
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可以参考官方文档