5
0

[Python×Selenium]ValueError: Timeout value connect was <object object at >, but it must be an int, float or None.が出たときの対処法

Last updated at Posted at 2024-03-14

状況

PythonとSeleniumを使用してスクレイピングを行おうと試みた.
Pythonファイル実行時にValueError: Timeout value connect was <object object at >, but it must be an int, float or None.とエラーが出た.

エラー発生時の環境

MacBook Air m1
anaconda 23.9.0
selenium 3.141.0
urllib3 2.0.3
python 3.10.13

エラー概要

実行すると以下のエラーが発生

Traceback (most recent call last):
  File "/Users/xxx/src/py/scripting/sample.py", line 4, in <module>
    browser = webdriver.Chrome()
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/_request_methods.py", line 118, in request
    return self.request_encode_body(
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/_request_methods.py", line 217, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/poolmanager.py", line 432, in urlopen
    conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/poolmanager.py", line 303, in connection_from_host
    return self.connection_from_context(request_context)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/poolmanager.py", line 328, in connection_from_context
    return self.connection_from_pool_key(pool_key, request_context=request_context)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/poolmanager.py", line 351, in connection_from_pool_key
    pool = self._new_pool(scheme, host, port, request_context=request_context)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/poolmanager.py", line 265, in _new_pool
    return pool_cls(host, port, **request_context)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/connectionpool.py", line 196, in __init__
    timeout = Timeout.from_float(timeout)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/util/timeout.py", line 190, in from_float
    return Timeout(read=timeout, connect=timeout)
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/util/timeout.py", line 119, in __init__
    self._connect = self._validate_timeout(connect, "connect")
  File "/Users/xxx/opt/anaconda3/envs/scrapy/lib/python3.10/site-packages/urllib3/util/timeout.py", line 156, in _validate_timeout
    raise ValueError(
ValueError: Timeout value connect was <object object at 0x7f96f80d07a0>, but it must be an int, float or None.

エラーメッセージによれば、urllib3ライブラリがタイムアウト値の形式を正しく解釈できないことが原因のようなものです。
このようなエラーが発生する要因はurllib3とseleniumのバージョンの依存関係に問題があると考えられます。

解決方法

urllib3とseleniumのバージョンの依存関係に問題については以下の記事が参考になります.

※上記の記事は2023/5/4のものなので現在の最新バージョンとは異なります.

以下の記事を見ると,

Can't install the latest version of urllib3 (2.0.1) because >selenium 4.9.0 depends on urllib3[socks]~=1.26.

The latest version of urllib3 is 2.0.1.

Now that requests 2.30.0 has been released, which supports >the latest urllib3, it's now selenium that's holding things >back. urllib3[socks]~=1.26 should be changed to >urllib3[socks]>=1.26 (or similar) to allow the latest version >of requests.

とあります.
私はアメリカ語がわからないのでDeepLで翻訳すると

selenium 4.9.0 が urllib3[socks]~=1.26 に依存しているため、最新バージョンの urllib3 (2.0.1) をインストールできません。

urllib3の最新バージョンは2.0.1です。

urllib3[socks]~=1.26は、urllib3[socks]>=1.26(または同様のもの)に変更されるべきです。

記事を観た上で自身の環境のseleniumとurllib3のバージョンを確認してみましょう.
インストール済みのライブラリのバージョンはlistコマンドで確認できます.
私はcondaを利用しているので以下のコマンドを使用
(activateしていることが前提)

conda list

pipを使用している方は以下のコマンドを使用して下さい

pip list

私の環境では以下の出力がされました.

# packages in environment at /Users/xxx/opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
...
selenium                  3.141.0         py39h9ed2024_1000  
...
urllib3                   2.0.3            py39hecd8cb5_0  
...

出力結果を見るとseleniumのバージョンが3.141.0,urllib3のバージョンが2.0.3であることがわかります.
つまり,seleniumのバージョンに対してurllib3のバージョンが新しすぎることがわかります。
よって,seleniumのより新しいバージョンをインストールし直すことで問題が解決します。

seleniumをバージョンを指定してインストール

私はcondaを利用しているので以下のコマンドを使用
(activateしていることが前提)

conda install selenium==4.9.0

pipを使用している方は以下のコマンドを使用して下さい

pip install selenium==4.9.0

インストールが完了したらlistコマンドを使用して指定したバージョンがインストールされているか確認しましょう。

conda list

pipを使用している方は以下のコマンドを使用して下さい

pip list

私の環境では以下の出力がされました.

# packages in environment at /Users/xxx/opt/anaconda3/envs/scrapy:
#
# Name                    Version                   Build  Channel
...
selenium                  4.9.0                    pypi_0    pypi
...
urllib3                   1.26.18                  pypi_0    pypi
...

指定したバージョンが正しくインストールされていますね🥳
Pythonファイルを実行してエラーが発生しなければ解決です🥳🥳
お疲れ様です🥳🥳🥳

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0