SeleniumVBA 設定ファイル(SeleniumVBA.ini)の生成とmsedgedriver.exeのパスのパス指定方法等

VBA

個人的にめちゃくちゃハマってしまったので、だれかの役に立つかもしれないメモ

SeleniumVBAがすごそうでソースを眺めたり遊んでいたら、 設定ファイル(iniファイル)が出力できることに気が付いた。

WebDriverクラスのCreateSettingsFileをたたくと、実行したExcelファイルと同じ階層に、SeleniumVBA.iniが作成される。

1.SeleniumVBA.ini 出力コード

Sub CreateIniFileSample()
    Dim driver As WebDriver
 
    Set driver = New_WebDriver
    driver.CreateSettingsFile

End Sub
GitHub - GCuser99/SeleniumVBA: A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows
A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows - GCuser99/Seleni...

SeleniumVBA.ini 生ファイル

VBAでここまで作りこんでいる。本当にすごい

# This settings file is completely optional. For it to have effect,
# it must be located in the same folder as the SeleniumVBA code
# library, and be named "SeleniumVBA.ini".
 
# If a value for an entry is not specified, then the system
# default value will be used.
 
# Note that all path-type entry values recognize the %[Environ]% syntax.
 
# A few useful Environ values for reference:
 
# %USERPROFILE%=C:\Users\[user name]
# %APPDATA%=C:\Users\[user name]\AppData\Roaming
# %LOCALAPPDATA%=C:\Users\[user name]\AppData\Local
# %TEMP%=C:\Users\[user name]\AppData\Local\Temp
 
[GENERAL]
 
# The driver_location_folder system defaults to Downloads folder.
# The default_io_folder system defaults to the active vba project's
# document location - leave this blank to use default.
# Valid values for command_window_style are vbHide (default), 
# vbNormalFocus, vbMinimizedFocus, vbMaximizedFocus, vbNormalNoFocus,
# and vbMinimizedNoFocus.
# The system default values for implicit_wait, page_load_timeout, and
# script_timeout are 0, 300000, and 30000 ms respectively.
 
driver_location_folder=%USERPROFILE%\Downloads
default_io_folder=
command_window_style=vbHide
 
implicit_wait=0
page_load_timeout=300000
script_timeout=30000
 
[AUTO-DRIVER-UPDATE]
 
# If auto_detect_and_update=True (system default) then everytime
# the WebDriver's Start* method is called, the Selenium driver's
# version is checked against the corresponding browser version.
# If the driver is not compatible with browser, it will be updated.
# min_compatibility_level determines trigger for updating an
# an out-of-date driver. System default is svbaBuildMajor.
# Use svbaMinor for less frequent updating, and svbaExactMatch
# for more frequent updating.
 
auto_detect_and_update=True
min_compatibility_level=svbaBuildMajor
 
# Below are browser-specific initializations.
# To automatically initialize a set of capabilities each time the
# OpenBrowser method of WebDriver class is invoked, set the
# preload_capabilities_file_path entry to the path of a valid json
# capabilities file. Note that if preload_capabilities_file_path is
# set to a blank value, or the entry is missing or commented out,
# then this option is ignored. Use the SaveToFile method of the
# WebCapabilities class to save a default set of capabilities
# for pre-loading.
# The system defaults for local_host_port:
# Chrome - 9515, Edge - 9516, Firefox - 4444, IE - 5555
 
[CHROME]
 
preload_capabilities_file_path=
local_host_port=9515
 
[EDGE]
 
preload_capabilities_file_path=
local_host_port=9516
 
[FIREFOX]
 
preload_capabilities_file_path=
local_host_port=4444
 
[INTERNET EXPLORER]
 
preload_capabilities_file_path=
local_host_port=5555
 
[PDF_DEFAULT_PRINT_SETTINGS]
 
# Valid units values are svbaInches (default) or svbaCentimeters.
# Valid orientation values are svbaPortrait (default) or svbaLandscape.
 
units=svbaInches
page_height=11
page_width=8.5
margin_bottom=.393701
margin_top=.393701
margin_right=.393701
margin_left=.393701
background=False
orientation=svbaPortrait
print_scale=1
shrink_to_fit=True

SeleniumVBA.iniで、msedgedriver.exeの格納先ファイルパスを指定

ソースを見ていたら、driver_location_folderがmsedgedriver.exe等、webdriverの格納先ファイルパスを探すようだ。

driver_location_folder=%USERPROFILE%\Downloads

%USERPROFILE%\Downloads に例えば”C:\Users\papanda925\Documents\VBA_Local”等の直接パスを指定し、各ブラウザのwebdriverをダウンロード格納しておけば、SeleniumVBAが自動的にドライバを取得する処理をスキップできる。実務でインターネットに接続することもできない立場の方には、この設定は必要そう。

driver_location_folder="C:\Users\papanda925\Documents\VBA_Local"

他、そもそもwebdriverクラスのStartxx(例:StartEdge)を実行する際に第1引数でも絶対パス指定ができるが、外部ファイルで制御したい場合はこちらもおすすめ

その他、SeleniumVBA.iniでおすすめそうな設定

デバッグ時だけかも

command_window_style=vbHide

 を 

command_window_style=vbMinimizedNoFocus

等の表示モードにすると、デバッグ時にwebdriverのプロンプトが表示されて便利(とくにプロセスをタスクマネージャーから落とすようなことを事態になるデバッグ発生時)

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

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