selenium」タグアーカイブ

ModuleNotFoundError: No module named ‘selenium’ seleniumモジュールのインストール方法

ModuleNotFoundError: No module named ‘selenium’

以前、seleniumを使ったスクリプトを使っていたのですが、久しぶりにそのスクリプトを実行しようとしたところ、

ModuleNotFoundError: No module named 'selenium'

というエラーが出てしまいました。なんでなくなったのかよくわかりませんが、再度モジュールをインストールする必要がありそうです。jupyternotebookの中で、

pip install selenium

として実行したら、

Collecting selenium
  Downloading selenium-4.8.0-py3-none-any.whl (6.3 MB)Note: you may need to restart the kernel to use updated packages.

Collecting trio~=0.17
  Downloading trio-0.22.0-py3-none-any.whl (384 kB)
Requirement already satisfied: certifi>=2021.10.8 in c:\programdata\anaconda3\lib\site-packages (from selenium) (2021.10.8)
Requirement already satisfied: urllib3[socks]~=1.26 in c:\programdata\anaconda3\lib\site-packages (from selenium) (1.26.7)
Collecting trio-websocket~=0.9
  Downloading trio_websocket-0.9.2-py3-none-any.whl (16 kB)
Requirement already satisfied: sortedcontainers in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (2.4.0)
Requirement already satisfied: cffi>=1.14 in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.14.6)
Requirement already satisfied: attrs>=19.2.0 in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (21.2.0)
Requirement already satisfied: idna in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (3.2)
Requirement already satisfied: sniffio in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.2.0)
Requirement already satisfied: async-generator>=1.9 in c:\programdata\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.10)
Collecting outcome
  Downloading outcome-1.2.0-py2.py3-none-any.whl (9.7 kB)
Collecting exceptiongroup>=1.0.0rc9
  Downloading exceptiongroup-1.1.0-py3-none-any.whl (14 kB)
Requirement already satisfied: pycparser in c:\programdata\anaconda3\lib\site-packages (from cffi>=1.14->trio~=0.17->selenium) (2.20)
Collecting wsproto>=0.14
  Downloading wsproto-1.2.0-py3-none-any.whl (24 kB)
Requirement already satisfied: PySocks!=1.5.7,<2.0,>=1.5.6 in c:\programdata\anaconda3\lib\site-packages (from urllib3[socks]~=1.26->selenium) (1.7.1)
Collecting h11<1,>=0.9.0
  Downloading h11-0.14.0-py3-none-any.whl (58 kB)
Installing collected packages: outcome, h11, exceptiongroup, wsproto, trio, trio-websocket, selenium
Successfully installed exceptiongroup-1.1.0 h11-0.14.0 outcome-1.2.0 selenium-4.8.0 trio-0.22.0 trio-websocket-0.9.2 wsproto-1.2.0

というメッセージが表示されて、あっさりインストールできました。コマンドウインドウとかでやる必要はなかったんですね。

 This version of ChromeDriver only supports

これで動くかと思いきや、今度は、

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 96
Current browser version is 109.0.5414.120

というエラー。これはお馴染みで、ChromeDriverが古いよということです。ウェブブラウザが勝手に自動更新しているのでChromeDriverはそのたびに手作業で新しくしないといけないのが面倒。

https://chromedriver.chromium.org/downloads

から自分のクロームのバージョンに合ったドライバーのバージョンをダウンロードしました。ダウンロードしたchromedriver.exeをスクリプトと同じフォルダに移動させました。

AttributeError: ‘WebDriver’ object has no attribute

これでスクリプトは実行できましたが、今度は、

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

というエラー。以前は動いたスクリプトですが、ドライバーのバージョンが変わったらダメになったようです。

AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'

別のスクリプトでも同様のエラーが。なんてこった。以前動いたプログラムが動かなくなるのは非常にストレスです。なぜなら自分の作業の再現性が取れないから。

Seleniumのバージョン変更に伴う仕様の変更に関してはネット上で解説記事がありました。

  1. Selenium4ではfind_element_by_id、nameは非推奨 2022年5月23日 2022年9月19日
  2. Seleniumで「WebDriver object has no attribute」が起きた時の対策と解決法 2023.01.14 2022.08.20
  3. 【Selenium】急にAttributeError: ‘WebDriver’ object has no attributeが起きた 投稿日 2022年07月04日 更新日 2022年07月04日  Qiita
  4. https://stackoverflow.com/questions/72773206/selenium-python-attributeerror-webdriver-object-has-no-attribute-find-el

プログラムをせっかく頑張って書いても、こうやってバージョンアップに伴って動かなくなっていくんですよね。これくらいの変更ならたいしたことないですが、自分が15年以上前に書いたLabVIEWのプログラムなんてもう修正する気が起きません。

さて、修正してみますか。まずこの1行が必要です。

from selenium.webdriver.common.by import By

  1. https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html

あとはネット記事を参照します。

書き直す前の文

elem_search = browser.find_element_by_id(‘user-search-text’)

書き直した後の文

elem_search = browser.find_element(By.ID,‘user-search-text’)

まあちょっと修正するだけなので、どうってことなかったです。これで以前と同じようにスクリプトが動作しました。