iT邦幫忙

0

python執行selenium出現錯誤

  • 分享至 

  • xImage

以下是我的code:

from selenium import webdriver
PATH = "C:/Users/user/Desktop/craw/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)
  • selenium版本為4.10.0
  • Google Chrome版本為114.0.5735.134
  • PATH內的chromedriver也是下載114的版本

出現的問題:
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

查了很久還是找不到原因,想請問各位不知道該怎麼解決呢?

看更多先前的討論...收起先前的討論...
ldd77 iT邦新手 5 級 ‧ 2023-06-28 14:41:52 檢舉
試試看把chromedriver.exe丟到python的資料夾裡面? 這樣就不用設PATH?
froce iT邦大師 1 級 ‧ 2023-06-28 14:42:12 檢舉
https://dev.to/codemee/selenium-4-de-shi-yong-3o0g

簡單的說你參考的code是3.0的,降版本就沒問題了。
我嘗試把chromedriver.exe丟到與python同路徑中,一樣會出現問題
而把selenium降到3.0版本,都能正確執行完畢了!
非常感謝ldd77、froce兩位大大的回答!!!
讓我能繼續操作下去~~~~
froce iT邦大師 1 級 ‧ 2023-06-28 16:39:59 檢舉
webdriver應該也要降版。
要不然就是selenium昇4.6以後參考我給你的連結讓他自動下載webdriver就好。
目前依照剛剛的降到3.0版本,
就會一直出現下面那堆錯誤,但如果最後加上driver.quit()就不會,
不確定是哪邊出問題QQ
我現在也同步試試看升級到4.6版本好了

DevTools listening on ws://127.0.0.1:1464/devtools/browser/c07c594d-e8cc-4e82-822c-fbaf0e2957eb
Exception ignored in: <function Service.__del__ at 0x0000020729178360>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\service.py", line 139, in stop
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\service.py", line 110, in send_remote_shutdown_command
ImportError: sys.meta_path is None, Python is likely shutting down
PS C:\Users\user\Desktop\craw> [22412:19316:0628/170746.041:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -101
[22412:19316:0628/170746.049:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -101
[22412:19316:0628/170747.455:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
[22412:19316:0628/170747.459:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
[22412:19316:0628/170748.254:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -101
[22412:19316:0628/170748.258:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
[22412:19316:0628/170749.057:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -101
[22412:19316:0628/170749.060:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
[22412:19316:0628/170758.645:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
[22412:19316:0628/170800.562:ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -100
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
kennex_x
iT邦新手 4 級 ‧ 2023-06-29 12:09:00

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

0
SK.
iT邦新手 5 級 ‧ 2024-05-24 11:52:10

如果沒有特別指定要chrome的話
其實edge也是不錯的選擇
(只是單純chrome driver 找不到我chrome的版本)/images/emoticon/emoticon02.gif

#WebDriver 測試
from selenium import webdriver

# 創建 Edge WebDriver 
driver = webdriver.Edge()
print(type(driver))

# 打開網頁
driver.get('https://www.example.com/')

我要發表回答

立即登入回答