iT邦幫忙

0

python selenium問題

  • 分享至 

  • xImage
from selenium import webdriver
driverPath = "C:/Users/ajack/.spyder-py3/chromedriver.exe"
browser = webdriver.Chrome(driverPath)
print(type(browser))

出現了
ValueError: Timeout value connect was <object object at 0x000001E91A1B4950>, but it must be an int, float or None.
想請問應該是要怎麼解決?
https://ithelp.ithome.com.tw/upload/images/20240519/20145023YmsyIXUJeq.png
ChromeDriver路徑是正確的

以下是完整的錯誤訊息
Traceback (most recent call last):

File ~\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)

File c:\users\ajack.spyder-py3\temp.py:4
browser = webdriver.Chrome(driverPath)

File ~\anaconda3\Lib\site-packages\selenium\webdriver\chrome\webdriver.py:76 in init
RemoteWebDriver.init(

File ~\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:157 in init
self.start_session(capabilities, browser_profile)

File ~\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:252 in start_session
response = self.execute(Command.NEW_SESSION, parameters)

File ~\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:319 in execute
response = self.command_executor.execute(driver_command, params)

File ~\anaconda3\Lib\site-packages\selenium\webdriver\remote\remote_connection.py:374 in execute
return self._request(command_info[0], url, body=data)

File ~\anaconda3\Lib\site-packages\selenium\webdriver\remote\remote_connection.py:397 in _request
resp = self._conn.request(method, url, body=body, headers=headers)

File ~\anaconda3\Lib\site-packages\urllib3_request_methods.py:118 in request
return self.request_encode_body(

File ~\anaconda3\Lib\site-packages\urllib3_request_methods.py:217 in request_encode_body
return self.urlopen(method, url, **extra_kw)

File ~\anaconda3\Lib\site-packages\urllib3\poolmanager.py:432 in urlopen
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

File ~\anaconda3\Lib\site-packages\urllib3\poolmanager.py:303 in connection_from_host
return self.connection_from_context(request_context)

File ~\anaconda3\Lib\site-packages\urllib3\poolmanager.py:328 in connection_from_context
return self.connection_from_pool_key(pool_key, request_context=request_context)

File ~\anaconda3\Lib\site-packages\urllib3\poolmanager.py:351 in connection_from_pool_key
pool = self._new_pool(scheme, host, port, request_context=request_context)

File ~\anaconda3\Lib\site-packages\urllib3\poolmanager.py:265 in _new_pool
return pool_cls(host, port, **request_context)

File ~\anaconda3\Lib\site-packages\urllib3\connectionpool.py:196 in init
timeout = Timeout.from_float(timeout)

File ~\anaconda3\Lib\site-packages\urllib3\util\timeout.py:190 in from_float
return Timeout(read=timeout, connect=timeout)

File ~\anaconda3\Lib\site-packages\urllib3\util\timeout.py:119 in init
self._connect = self._validate_timeout(connect, "connect")

File ~\anaconda3\Lib\site-packages\urllib3\util\timeout.py:156 in _validate_timeout
raise ValueError(

ValueError: Timeout value connect was <object object at 0x000001E91A1B4950>, but it must be an int, float or None

念戀 iT邦新手 4 級 ‧ 2024-05-20 14:56:24 檢舉
GPT:
如果 browser = webdriver.Chrome() 可以正常執行,通常是因為你的系統已經將 chromedriver 添加到環境變量(PATH)中,或者 chromedriver 已經在 Selenium 預設的搜尋路徑中。Selenium 在嘗試啟動 Chrome 瀏覽器時會自動找到 chromedriver 可執行文件。

想說我都沒加那個路徑!?
twtabc iT邦新手 5 級 ‧ 2024-05-20 18:29:43 檢舉
也許可行?

[selenium - What is default location of ChromeDriver and for installing Chrome on Windows - Stack Overflow](https://stackoverflow.com/questions/49788257/what-is-default-location-of-chromedriver-and-for-installing-chrome-on-windows)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
SK.
iT邦新手 5 級 ‧ 2024-05-20 09:17:56
最佳解答

1.可能是下載的 chromedriver 支援版本和你的 chrome 版本不同
2.嘗試更新 selenium

題外話
我一開始也是用chrome來做爬蟲 也是遇到很多問題(特別是找不到支援我chrome版本的driver)
還要把chrome降版本甚麼的 也看到別人說chrome很吃資源
後來就改用Edge做了 做的挺順利的 供參/images/emoticon/emoticon08.gif

#WebDriver 測試
from selenium import webdriver

driver = webdriver.Edge()
print(type(driver))

# 打開網頁
driver.get('https://www.example.com/')
Kailis iT邦研究生 1 級 ‧ 2024-05-20 14:25:41 檢舉

"後來就改用Edge做了 做的挺順利的 供參"
這句很重要..感謝!

jackkjh iT邦新手 5 級 ‧ 2024-05-20 21:50:49 檢舉

感謝您提供的意見用Edge做真的比較順利!但作業要用chrome

這是我修改後的chrome程式,加兩行程式,就可以開啟了

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

driverPath = "C:/Users/ajack/.spyder-py3/chromedriver.exe"
service = Service(driverPath)

browser = webdriver.Chrome(service=service)
print(type(browser))
0
Dino
iT邦新手 5 級 ‧ 2024-05-20 17:42:11

個人覺得在selenium上使用Chrome的時候版本問題會很令人頭痛,如果怕麻煩其實我覺得firefox跟edge都是很好的選擇。

但如果還是很想用Chrome,分享一下方法

可以下載測試用的Chrome之後指定路徑,就可以固定版本不會受到電腦中的Chrome的更新影響
https://developer.chrome.com/blog/chrome-for-testing?hl=zh-tw

可以透過這個方式指定執行的Chrome路徑
附上selenium文件

chromeOptions = webdriver.ChromeOptions()
chromeOptions.binary_location = f"{driver_path}/chrome-linux64/chrome"
driver = webdriver.Chrome(executable_path=f"{driver_path}/chromedriver-linux64/chromedriver", options=chromeOptions)
jackkjh iT邦新手 5 級 ‧ 2024-05-20 21:51:44 檢舉

感謝您提供的意見

這是我修改後的chrome程式,加兩行程式,就可以開啟了

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

driverPath = "C:/Users/ajack/.spyder-py3/chromedriver.exe"
service = Service(driverPath)

browser = webdriver.Chrome(service=service)
print(type(browser))

我要發表回答

立即登入回答