[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")

后來查了之后發(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可以參考官方文檔

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容