以下是我的code:
from selenium import webdriver
PATH = "C:/Users/user/Desktop/craw/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)
出現的問題:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path
path = SeleniumManager().driver_location(options) if path is None else path
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 74, 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 "c:\Users\user\Desktop\craw\app.py", line 5, in
driver = webdriver.Chrome(PATH)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in init
self.service.path = DriverFinder.get_path(self.service, self.options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path
raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
查了很久還是找不到原因,想請問各位不知道該怎麼解決呢?
Dears,
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(executable_path = "chromedriver")
web = webdriver.Chrome(service=service)
記得 selenium
有所改變,用此方法實測可以成功執行。
selenium == 4.10.0
Chrome == 114.0.5735.199
如果沒有特別指定要chrome的話
其實edge也是不錯的選擇
(只是單純chrome driver 找不到我chrome的版本)
#WebDriver 測試
from selenium import webdriver
# 創建 Edge WebDriver
driver = webdriver.Edge()
print(type(driver))
# 打開網頁
driver.get('https://www.example.com/')