LoginSignup
7
3

More than 3 years have passed since last update.

Python + Seleniumで「Message: session not created」が発生した場合

Posted at

PythonでSeleniumを動かしている時に「Message: session not created」が発生して、気づいたら数時間エラー解決に掛かっていました。
このエラーをググると大体「Chromeのバージョン」と「Chrome-driverのバージョン」違いで発生するという情報が出てきますが、今回自分が経験したパターンは異なっていたので、備忘録として残しておきたいと思います。

[root@v111-111-111-11 html]# python3 run.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: Unable to receive message from renderer
  (Session info: headless chrome=80.0.3987.122)

エラー原因

バージョンも両方一致しているし、バージョン変更していないし、以前まで動いていたから、何かChromeのプロセスが大量に溜まってたりしないかなと思って、実行しているサーバ上でChromeのプロセスを確認しました。

[root@v111-111-111-11 html]# pgrep -l chrome
10436 chrome
10444 chrome
[以下略]

ずらっとChromeのプロセスが50個くらい大量に残っていました。恐らくwebdriverの処理が終了した後にChromeのプロセスを終了できておらず、溜まってしまったのかと思います。

解決方法

[root@v111-111-111-11 html]# pkill chrome

Chromeのプロセスを終了させ、再度スクリプトを実行したところ、問題なく動きました。
「Chromeのバージョン」と「Chrome-driverのバージョン」が一致しているにも関わらず、上記エラーが発生する場合は参考にしてみてください。

7
3
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
7
3