月別アーカイブ: 2023年2月

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’)

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

自分が過去につくった.jpnbファイルを探すプログラム

JupyterNotebookをたくさんつくると、どの作業をするのにつかったプログラムファイルがどれだったかわからなくなってきます。何か手がかりになる検索語があれば、その単語を含むスクリプトを検索するということが、可能です。

自分の場合、そのスクリプトで作業した結果をエクセルファイルなどに出力していることが多いので、その出力ファイル名を手掛かりに、その出力ファイルを作る作業をさせたプログラムを探すことができます。

# JupyterNotebookSearch.jpnb
# 目的:自分が過去につくったスクリプトを、スクリプト内で使った単語で検索

import glob

filenames = ‘*.ipynb’
query = ‘東京’

for filepath in glob.iglob(filenames, recursive=True):
with open(filepath, encoding=’utf-8′) as file:
s = file.read()
if (s.find(query) > -1):
print(filepath)#検索語をスクリプト内に含むファイル名を列挙

PDFの各ページを自動的にスクリーンキャプチャする方法 python programming

保護されたPDFの場合、一定期間が経過すると閲覧できなくなるものがあります。しかしどうしても手元に記録として残しておきたい場合もあるかもしれません。そんな場合の苦肉の策としてPDFをスクリーンキャプチャしておくということが考えられます。しかしそのPDFが何百ページもあると手作業で行うことは現実的でありません。そこでpythonプログラミングで自動的にスクリーンキャプチャするコードを作りました。

# PDFをスクリーンキャプチャー
# アドビアクロバットプロでPDFを開いている前提。それとJupyterを動かしているウェブブラウザ以外は、アプリケーションやウインドウは開かないこと。
# http://python.geo.jp/

import pyautogui
from datetime import datetime
import time

pyautogui.hotkey(‘alt’, ‘esc’)#PDFのウインドウを前に
pyautogui.hotkey(‘alt’,’v’)#アドビアクロバットプロ メニューで「表示」を選択全画面表示
pyautogui.keyDown(‘f’)#アドビアクロバットプロ 全画面表示
time.sleep(5)#needs enough time 最初はこれくらい必要

for i in range(100):#PDFのページの全ページ数の数字を入れること

screen_shot = pyautogui.screenshot() #スクリーンキャプチャー
t = datetime.now()
current_time = t.strftime(“%Y%m%d_%H%M%S_”)#現在時刻をファイル名に使うため

#画像を保存 保存したければコメントアウトすること
# screen_shot.save(current_time+str(t.microsecond)+’.png’)

pyautogui.press(‘enter’)#画面を次のページに
time.sleep(1)#needs enough time 1秒で十分だった。0.5秒でもよかったかも。