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.
想請問應該是要怎麼解決?
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
1.可能是下載的 chromedriver 支援版本和你的 chrome 版本不同
2.嘗試更新 selenium
題外話
我一開始也是用chrome來做爬蟲 也是遇到很多問題(特別是找不到支援我chrome版本的driver)
還要把chrome降版本甚麼的 也看到別人說chrome很吃資源
後來就改用Edge做了 做的挺順利的 供參
#WebDriver 測試
from selenium import webdriver
driver = webdriver.Edge()
print(type(driver))
# 打開網頁
driver.get('https://www.example.com/')
"後來就改用Edge做了 做的挺順利的 供參"
這句很重要..感謝!
感謝您提供的意見用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))
個人覺得在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)