3
1

More than 1 year has passed since last update.

Selenium + Python で EdgeのIEモードでスクレイピング

Posted at

Selenium + Python で EdgeのIEモードでスクレイピング

ある程度の前提はあるものとして、詳細は略します。
環境は、Windows10 Pro 64bit とSelenium4.1.2。

Pythonインストール

Pythonのサイトからダウンロードする。64bit版でよい。
管理者としてインストール。
pyLauncherを入れた方が良いかもしれない。
全てのユーザ向けにインストールする事。
「.py」ファイルが、OSで「Python File」として認識されている事を確認。

確認事項

「.py」ファイルが、OSで「Python File」として認識されている事を確認。

もし、Pythonファイルの紐づけがおかしかった場合、
管理者権限でコマンドスクリプトを起動して、以下のコマンドによって関連付けが可能。
assoc .py=Python.File
assoc .pyo=Python.CompiledFile
assoc .pyw=Python.NoConFile
ftype Python.File="C:\Program Files\Python39\python.exe" "%1" %*
ftype Python.CompiledFile="C:\Program Files\Python39\python.exe" "%1" %*
ftype Python.NoConFile="C:\Program Files\Python39\pythonw.exe" "%1" %*
Windowsの関連付け設定が残っていたら動かないので、レジストリエディタを開いて以下のキーを削除。
HKEY_CURRENT_USER\Software\Classes.[拡張子]
HKEY_CURRENT_USER\Software\Classes[拡張子]_auto_file
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.[拡張子]

VNCのインストール

専用にPCを用意して、他のPCから進捗だけを確認する場合の話。
画面を隠して実行可能なら不要だが、そうでない場合はVNC経由の方が無難。
仮想サーバなどで実行する場合、VNCServerを実行サーバに、VNC Viewerを接続元PCに入れる。
ex) TightVNC Server、Ultra VNC Viewer

スクリーンロック

スクリーンロックは解除する。
専用のPCまたは仮想サーバのスクリーンセーバは停止しておくこと。
動作中はログインを継続する必要がある、リモートデスクトップを使用した場合に接続を切る事はログアウトを意味する、スクリーンセーバの起動もログアウトを意味する為。

WebDriver

Edge用WebDriver

EDGEのバージョンを調べる。
下記URLから64bit版の同じバージョンのWebDriverをダウンロードする。
https://developer.microsoft.com/ja-jp/microsoft-edge/tools/webdriver/
解凍して、PATHの通ったフォルダにmsedgedriver.exeを置く。
ex) C:\Windows, C:\Program Files\Python39\Scripts\

IE用WebDriver

Seleniumのサイトから、IE Web Driverの32bit版をダウンロードする。64bit版は動かないので注意。
解凍して、PATHの通ったフォルダにmsedgedriver.exeを置く。
ex) C:\Windows, C:\Program Files\Python39\Scripts\

pipそのもののインストール

pipがSeleniumのインストールに必要ではある。
ただし、Pythonをインストールすれば自動的にインストールされている筈。

pipによるSelenium他のインストール

コマンドプロンプトを管理者として起動する。

この辺りを入れた。順番に注意する事。
実行してみて、module not foundをエラーが返った場合は、該当moduleを再インストール。

pip install pywinauto
pip install wheel
pip install pyautogui
pip install requests
pip install pysnooper
pip install pandas
pip install webdriver_manager
pip install msedge-selenium-tools
pip install xlrd
pip install openpyxl
pip install selenium==4.1.2

cmdは終了、もしくは再起動する。

Seleniumによるスクレイピングの設定。

Selenium+IEでスクレイピングする時に一般的に必要とされる設定項目を記載する。

The IEDriverServer exectuable must be downloaded and placed in your PATH.

下記2つのWebDriverを設置する。
・msedgedriver.exe
・IEDriverServer.exe -> 32bit版を置く事。

On IE 7 or higher on Windows Vista, Windows 7, or Windows 10, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".

概要としては、IE 保護モードを有効にする。
ただし、上記文面は少し誤解を招く文面で、GUIで表示されていた4つのゾーン共通で「IE保護モード」の有無を統一する事にあり、なおかつ一歩間違えるとIEが64bitで起動し作動不良を招くため説明に注意が必要。

元々は、こちらの下記の設定だったが、IEのEdge置き換え(WindowsUpdate)によりGUIより項目が消滅してた。

「コントロールパネル(表示方法: カテゴリ) -> ネットワークとインターネット -> インターネットオプション -> セキュリティ -> 全てのゾーンで保護モード有効化。 」

レジストリを修正し、IE保護モードを統一する。

設定内容自体は、HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
の0~4の、「2500」の値を全て「0」に変更する。

Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.

拡張保護モードを無効にする。
わかりにくい説明だが、要はIEを32bitで起動する設定を施す、である。IEを64bitで起動してはいけない。
以前は、IEのオプションの詳細設定からGUIで可能だった。

レジストリを修正し、IEを32bitで起動する設定を行う。

設定内容自体は、
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
に文字列で「Isolation」を作り(REG_SZになる)、値を「PMIL」にする。

拡張保護モードで64ビット プロセッサを有効にする

同じく、わかりにくい説明だが、要はIEを32bitで起動する設定を施す、である。IEを64bitで起動してはいけない。
以前は、IEのオプションの詳細設定からGUIで可能だった。

レジストリを修正し、IEを32bitで起動する設定を行う。

設定内容自体は、
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
にREG_DWORDでIsolation64Bitを作成し、値を0にする。

追加事項。

グループポリシーで「拡張保護モードを有効にする」及び「64 ビット バージョンの Windows の保護拡張モードで実行しているときに 64 ビット タブ プロセスを有効にする」が有効になっている事で
IEが64bitで起動する事がある。

gpedit.mscを起動。
下記の両方にて、
コンピューターの構成 > 管理用テンプレート > Windows コンポーネント > Internet Explorer > インターネット コントロール パネル > [詳細設定] ページ
ユーザーの構成 > 管理用テンプレート > Windows コンポーネント > Internet Explorer > インターネット コントロール パネル > [詳細設定] ページ

下記を設定する。
「拡張保護モードを有効にする」を無効に設定
「64 ビット バージョンの Windows の保護拡張モードで実行しているときに 64 ビット タブ プロセスを有効にする」を無効に設定
コマンドプロンプトにて、gpupdate /force を実行。

IEが32bitで起動しているか64bitで起動しているか確認するには起動するのが最も早い。

The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.

無関係、おそらく座標を利用する場合のみ。

For Windows 10, you also need to set "Change the size of text, apps, and other items" to 100% in display settings.

倍率を変えている場合は100%に戻す。

For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates.

For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE.

レジストリを修正し、WebDriverからIEを終了できる加工を行う。こちらは32bit用。ただしOSとブラウザのいずれを指しているか不明。

設定内容自体は、
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
を作成し、
DWORD作って名前はiexplorer、値は0を設定する。

For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE.

Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present.
Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

レジストリを修正し、WebDriverからIEを終了できる加工を行う。こちらは64bit用。ただしOSとブラウザのいずれを指しているか不明。

設定内容自体は、
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
を作成し、
DWORD作って名前はiexplorer、値は0を設定する。

インターネットオプション→セキュリティタブ→ローカルイントラネット→サイトをクリックし以下の通り設定してください。

「イントラネットの設定を自動的に検出する」のチェックを外す。
必要かどうか不明。

他、あててはいけないアップデートもある

コントロールパネル→プログラム→プログラムと機能→インストールされた更新プログラム
のMicrosoft Windows の赤に Microsoft Windows (KB3025390)
がないことを確認。
あると、動かないようなのであった場合はアンインストールする。

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