報錯信息
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")
后來查了之后發(fā)現(xiàn)新版本的Selenium已經(jīng)不用這樣寫了
直接改成下面即可,同理其他瀏覽器也是一樣, 不需要加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可以參考官方文檔