Python Selenium – SessionNotCreatedException

python

SessionNotCreatedException 対処方法

エラーが出る原因

Python言語 Selemiumを使用した【 スクレイピング / クローリング 】時に「SessionNotCreatedException」が、発生して webdriverの アップデート等で対応しておりませんか?

from selenium import webdriver
 
driver = webdriver.Chrome()
driver.get('https://google.com')

上記ような実装方法だと、Chromeがバージョンアップされると動かなくなります。
理由は、ローカル上にインストールされている「webdriver」と、実装に使用している Chromeのバージョンの違いによって引き起こされる問題です。

$ python main.py
Traceback (most recent call last):
File "/Users/username/Desktop/main.py", line 4, in <module>
driver = webdriver.Chrome()
--- 省略 ---
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83

これを使えば、もう面倒な修正はいらない!?【ChromeDriverManager】

【 概要 】
実行時に、毎度「webdriver」を自動更新してくれる。

「webdriver_manager」の、インストール

pip install webdriver-manager

上記コマンドを、ターミナル(Windowsの場合コマンドプロンプト)で入力します。

~ $ pip install webdriver-manager
Collecting webdriver-manager
Downloading https://files.pythonhosted.org/packages/32/28/a4e7638fc497ff8f86c6670a5f9f42dc018c37a0b254caa5e51799959da5/webdriver_manager-3.3.0-py2.py3-none-any.whl
Collecting crayons (from webdriver-manager)
Using cached https://files.pythonhosted.org/packages/5b/0d/e3fad4ca1de8e70e06444e7d777a5984261e1db98758b5be3e8296c03fe9/crayons-0.4.0-py2.py3-none-any.whl
Collecting configparser (from webdriver-manager)
Downloading https://files.pythonhosted.org/packages/08/b2/ef713e0e67f6e7ec7d59aea3ee78d05b39c15930057e724cc6d362a8c3bb/configparser-5.0.1-py3-none-any.whl
Collecting requests (from webdriver-manager)
Downloading https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 828kB/s
Collecting colorama (from crayons->webdriver-manager)
Downloading https://files.pythonhosted.org/packages/44/98/5b86278fbbf250d239ae0ecb724f8572af1c91f4a11edf4d36a206189440/colorama-0.4.4-py2.py3-none-any.whl
Collecting urllib3<1.27,>=1.21.1 (from requests->webdriver-manager)
Downloading https://files.pythonhosted.org/packages/23/fc/8a49991f7905261f9ca9df5aa9b58363c3c821ce3e7f671895442b7100f2/urllib3-1.26.3-py2.py3-none-any.whl (137kB)
100% |████████████████████████████████| 143kB 996kB/s
Collecting idna<3,>=2.5 (from requests->webdriver-manager)
Using cached https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests->webdriver-manager)
Downloading https://files.pythonhosted.org/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl (147kB)
100% |████████████████████████████████| 153kB 5.7MB/s
Collecting chardet<5,>=3.0.2 (from requests->webdriver-manager)
Downloading https://files.pythonhosted.org/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl (178kB)
100% |████████████████████████████████| 184kB 4.7MB/s
Installing collected packages: colorama, crayons, configparser, urllib3, idna, certifi, chardet, requests, webdriver-manager
Successfully installed certifi-2022.3.5 chardet-4.0.0 colorama-0.4.4 configparser-5.0.1 crayons-0.4.0 idna-2.10 requests-2.25.1 urllib3-1.26.3 webdriver-manager-3.3.0

~ $

「Successfully installed」と、表示されていれば完了です。

実装の修正(ChromeDriverManager)

冒頭で載せた実装内容を以下に書き換える。

## before
from selenium import webdriver
 
driver = webdriver.Chrome()
driver.get('https://google.com')

===========================================================

## after
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
 
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://google.com')

実行すると、以下のログがターミナル(Windowsの場合コマンドプロンプト)に表示され、実行時の Chromeバージョンと一致した webdriverが使用されている事が分かる。

====== WebDriver manager ======
Current google-chrome version is 98.0.4758
Get LATEST chromedriver version for 98.0.4758 google-chrome
Trying to download new driver from https://chromedriver.storage.googleapis.com/98.0.4758.102/chromedriver_mac64.zip
Driver has been saved in cache [/Users/noachan/.wdm/drivers/chromedriver/mac64/98.0.4758.102]
/Users/username/main.py:28: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

今後、上記で対応する事により自分自身で使用する分にはもちろん、他ユーザーに配布して使用する場合など保守性が高く「手動更新作業」から解放されます。

是非、お試し下さい。

comment 📝

タイトルとURLをコピーしました