今回は、PowerShellからMicrosoft Edgeで表示したWebページのセレクトボックスを操作する方法を紹介する。任意のアイテムを選択することはもちろん、どのアイテムが選択されているかの情報を得ることもできる。
→連載「PowerShell Core入門 - 基本コマンドの使い方」の過去回はこちらを参照。
操作に使うサンプルページ
今回は操作のサンプルページとして「https://getbootstrap.jp/docs/5.0/examples/cheatsheet/」を使用する。次のようなページだ。
このページの中程にセレクトボックスがあるので、これをPowerShellから操作する。
これまでに説明してきたように、スクリプトで操作するには対象となる要素のXPathが必要だ。次のように開発者ツールを起動して対象となる要素のXPathを調査する。
セレクトボックスはselect要素で構成され、ここの選択項目はoption要素で構成されている。それぞれ取り出すと次のようになる。
要素 | XPath |
---|---|
select | //∗[@id="sizing"]/div[2]/div[1]/div[2]/select |
option | //∗[@id="sizing"]/div[2]/div[1]/div[2]/select/option[1] |
option | //∗[@id="sizing"]/div[2]/div[1]/div[2]/select/option[2] |
option | //∗[@id="sizing"]/div[2]/div[1]/div[2]/select/option[3] |
option | //∗[@id="sizing"]/div[2]/div[1]/div[2]/select/option[4] |
これで準備は完了だ。
PowerShell Seleniumでselect要素を操作する
早速PowerShellからMicrosoft Edgeを操作してみよう。まず、Microsoft Edge WebDriverを起動して操作するMicrosoft Edgeを起動する(この起動スクリプト「webdriver_edge_start.ps1」は以前紹介したものから少しアップデートしたので、付録を参考にしていただきたい)。
PS C:\Users\daichi> webdriver_edge_start.ps1
Microsoft Edge WebDriverを起動します。
Microsoft Edge WebDriverの起動処理完了。
PS C:\Users\daichi>
次のようにWebDriver経由で操作可能なMicrosoft Edgeが起動する。
次に、先ほどのサンプルページをSet-SeUrlコマンドレットを使って開く。
PS C:\Users\daichi> Set-SeUrl -Url https://getbootstrap.jp/docs/5.0/examples/cheatsheet/
PS C:\Users\daichi>
次のようにサンプルページが表示される。
次に、セレクトボックスの選択を行う。セレクトボックスの選択は、select要素を取得してから、その要素に対してSet-SeSelectValueコマンドレットを使って行う。Set-SeSelectValueコマンドレットで選択対象を指定する際に、-Valueパラメータで要素の文字列を指定するところがポイントだ。value属性の値ではなく実際に表示されているテキストを指定する。
PS C:\Users\daichi> $Element = Get-SeElement -By XPath -Value '//*[@id="sizing"]/div[2]/div[1]/div[2]/select'
PS C:\Users\daichi> Set-SeSelectValue -Element $Element -Value 'One'
PS C:\Users\daichi>
すると、次のように指定した項目が選択された状態になる。
次に「Two」を選択してみる。実行と実行後の結果は次のようになる。
PS C:\Users\daichi> Set-SeSelectValue -Element $Element -Value 'Two'
PS C:\Users\daichi>
最後に「Three」を選択する。実行と実行後の結果は次のようになる。
PS C:\Users\daichi> Set-SeSelectValue -Element $Element -Value 'Three'
PS C:\Users\daichi>
今度はセレクトボックスの選択状態を調べる。調べる方法はこれまでに本連載で取り上げてきた方法と同じだ。option要素を取得して、その.Selectedメンバを調べればよい。次のようになる。
PS C:\Users\daichi> $Element = Get-SeElement -By XPath -Value '//*[@id="sizing"]/div[2]/div[1]/div[2]/select/option[1]'
PS C:\Users\daichi> $Element.Selected
False
PS C:\Users\daichi> $Element = Get-SeElement -By XPath -Value '//*[@id="sizing"]/div[2]/div[1]/div[2]/select/option[2]'
PS C:\Users\daichi> $Element.Selected
False
PS C:\Users\daichi> $Element = Get-SeElement -By XPath -Value '//*[@id="sizing"]/div[2]/div[1]/div[2]/select/option[3]'
PS C:\Users\daichi> $Element.Selected
False
PS C:\Users\daichi> $Element = Get-SeElement -By XPath -Value '//*[@id="sizing"]/div[2]/div[1]/div[2]/select/option[4]'
PS C:\Users\daichi> $Element.Selected
True
PS C:\Users\daichi>
確かに、最後の選択した「Three」が選択された状態になっていることがわかる。操作が終わったら、以下のようにWebDriverとMicrosoft Edgeを終了して操作は終了だ。
PS C:\Users\daichi> webdriver_edge_stop.ps1
動作しているMicrosoft Edge WebDriverを終了します。
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
23 8.29 23.86 0.00 39276 5 msedgedriver
動作しているMicrosoft Edge WebDriverの終了処理完了。
PS C:\Users\daichi>
少しずつでも仕事の自動化を進めよう
本連載でこれまでに紹介してきた操作で、入力フィールド、チェックボックス、ラジオボタン、セレクトボックスをPowerShellから操作できるようになった。これだけで入力操作を行う必要があるWebページの結構な部分をPowerShellから操作できることになる。これでもう、PowerShellスクリプトを書いておけば処理を自動化できるわけだ。
正しく動作させるまでの調査と実験には多少の時間がかかるものの、一旦使えるようになれば、その効果は絶大だ。少しずつでもこういった機能を利用し、仕事の効率化に取り組んでいこう。
付録
webdriver_edge_start.ps1
#!/usr/bin/env pwsh
#========================================================================
# Microsoft Edge WebDriverを起動する
#========================================================================
#========================================================================
# 動作しているMicrosoft Edge WebDriverをすべて終了
#========================================================================
webdriver_edge_stop.ps1
#========================================================================
# Seleniumモジュールがない場合にはインストール
#========================================================================
if (-Not (Get-InstalledModule -Name Selenium 2> $Null)) {
'Seleniumモジュールをインストールします。'
Install-Module -Name Selenium -AllowPrerelease -Force
Get-InstalledModule -Name Selenium
}
#========================================================================
# Microsoft Edge WebDriverを起動
#========================================================================
'Microsoft Edge WebDriverを起動します。'
$Size = '1200,800'
if (-Not (Start-SeDriver -Browser Edge -Size $Size 2> $Null 3> $Null))
{
#================================================================
# Microsoft EdgeとMicrosoft Edge WebDriverのバージョンが一致して
# いないためにドライバが動作しなかった可能性がある。
#================================================================
#================================================================
# 不要なドライバプロセスを終了
#================================================================
webdriver_edge_stop.ps1
#================================================================
# Microsoft Edgeのバージョン番号
#================================================================
$EdgeDir='C:\Program Files (x86)\Microsoft\Edge\Application\'
$EdgeVersion=( Get-ChildItem -Name $EdgeDir |
Where-Object { $_ -NotMatch "[a-zA-Z]+" } |
Select-Object -First 1 )
#================================================================
# Microsoft Edge WebDriverダウンロードURLとデプロイ先パス
#================================================================
$DriverURL="https://msedgedriver.azureedge.net/$EdgeVersion/edgedriver_win64.zip"
$SeModVer=(Get-InstalledModule -Name Selenium).Version -replace "-.+$",""
$DriverDir="$env:HOME\Documents\powershell\Modules\Selenium\$SeModVer\assemblies"
$DriverDownloadDir="$DriverDir\_download"
#================================================================
# WebDriverダウンロード用の一時ディレクトリを作成
#================================================================
New-Item $DriverDownloadDir -ItemType Directory -Force
#================================================================
# Microsoft Edgeと同じバージョンのMicrosoft Edge WebDriverを
# ダウンロード
#================================================================
"Microsoft Edge WebDriver version $EdgeVersion をダウンロードします。"
curl -get `
-o $DriverDownloadDir\edgedriver_win64.zip `
$DriverURL
#================================================================
# Microsoft Edge WebDriverをデプロイ
#================================================================
"Microsoft Edge WebDriver version $EdgeVersion をインストールします。"
Expand-Archive -Path $DriverDownloadDir\edgedriver_win64.zip `
-Destination $DriverDownloadDir `
-Force
Copy-Item -Path $DriverDownloadDir\msedgedriver.exe `
-Destination $DriverDir\msedgedriver.exe `
-Force
#================================================================
# WebDriverダウンロード用の一時ディレクトリを削除
#================================================================
Remove-Item $DriverDownloadDir -Recurse -Force
#================================================================
# Microsoft Edge WebDriverを起動する
#================================================================
if (-Not (Start-SeDriver -Browser Edge -Size $Size 2> $Null 3> $Null))
{
#========================================================
# 原因不明の起動不能
#========================================================
#========================================================
# 不要なドライバプロセスを終了
#========================================================
webdriver_edge_stop.ps1
Exit
}
}
'Microsoft Edge WebDriverの起動処理完了。'
Microsoft Edgeアップデートのタイミングには、更新前のバージョンと更新後のバージョンが情報として同時に存在するタイミングがある。前回までに起動PowerShellスクリプトはこの条件で処理を行うことができなかったため、そのタイミングでも動作するように上記PowerShellスクリプトはアップデートした。PowerShellスクリプトはこのようにすぐに問題に対処して書き換えられるところもポイントだ。
webdriver_edge_stop.ps1
#!/usr/bin/env pwsh
#========================================================================
# Microsoft Edge WebDriverを終了する
#========================================================================
#========================================================================
# WebDriverプロセスを終了
#========================================================================
if (Get-Process -Name msedgedriver 2> $Null)
{
'動作しているMicrosoft Edge WebDriverを終了します。'
Get-Process -Name msedgedriver 2> $Null
# Microsoft Edge WebDriverを終了
Stop-SeDriver 2> $Null
# まだ動作しているほかのMicrosoft Edge WebDriverを終了
if (Get-Process -Name msedgedriver 2> $Null)
{
Get-Process -Name msedgedriver 2> $Null | Stop-Process
}
'動作しているMicrosoft Edge WebDriverの終了処理完了。'
}