【PowerShell】Selenium-Module New-SeScreenshot スクリーンショットの取得と保存

目次

概要

 起動しているブラウザ画面のスクリーンショットを取得し、保存までまとめて実行するメソッドです。スクリーンショットの取得と保存を分けたい場合は、Invoke-SeScreenshot(取得)とSave-SeScreenshot(保存)に分けて実行します。

 PowerShellでSeleniumを利用するに当たって、ある程度簡易的にSeleniumを利用できる様に専用のモジュールがGitHubに用意されています。いきなりPowerShellでSeleniumを扱うより、Seleniumを-Moduleを利用した方が簡単にPowerShellでSleniumを利用することができます。環境構築するには、以下ページを参照してみてください。

コマンド(Commond)

New-SeScreenshot

代替コマンド(Alias)

SeScreenshot

構文(Syntax)

  • New-SeScreenshot [-Path] <Object> [[-ImageFormat] <ScreenshotImageFormat>] [-Target <Object>] [<CommonParameters>]
  • New-SeScreenshot [[-Path] <Object>] [[-ImageFormat] <ScreenshotImageFormat>] -PassThru [-Target <Object>] [<CommonParameters>]
  • New-SeScreenshot -AsBase64EncodedString [-Target <Object>] [<CommonParameters>]

オプション

Name引数型解説Short Example
PathObject保存先のフルパスを文字列で指定New-SeScreenshot -Target $Driver -ImageFormat Png -Path $DocumentDir"\PowerShell\programan.png"
ImageFormatScreenshotImageFormat画像形式を"Bmp","Gif","Jpeg","Png","Tiff"から指定New-SeScreenshot -Target $Driver -ImageFormat Png -Path $DocumentDir"\PowerShell\programan.png"
TargetObjectドライバーやWeb要素を指定New-SeScreenshot -Target $Driver -ImageFormat Png -Path $DocumentDir"\PowerShell\programan.png"
PassThruオブジェクトプロパティ情報を返すNew-SeScreenshot -Target $Driver -ImageFormat Png -Path $DocumentDir"\PowerShell\programan.png" --PassThru
AsBase64EncodedStringBase64にエンコードされた文字列を返すNew-SeScreenshot -Target $Driver --AsBase64EncodedString
CommonParameters共通パラメータ

※ShortExampleは、動作確認ができたコードを記載しています。

Example

$shell = New-Object -ComObject Shell.Application
$DocumentDir = $shell.NameSpace("shell:Personal").Self.Path

$Driver = Start-SeChrome -Quiet
Enter-SeUrl https://programan.org/ -Driver $Driver

$Element = Get-SeElement -Target $Driver -ClassName "newmark" -Timeout 10
$actions = New-Object OpenQA.Selenium.Interactions.Actions($Driver)
$actions.MoveToElement($Element[0]).Perform()

New-SeScreenshot -Target $Driver -ImageFormat Png -Path $DocumentDir"\PowerShell\programan.png" -PassThru
New-SeScreenshot -Target $Driver -AsBase64EncodedString

Stop-SeDriver $Driver

Module

New-SeScreenshotのSelenium-Module構文を以下に掲載します。

function New-SeScreenshot {
    [Alias('SeScreenshot')]
    [cmdletbinding(DefaultParameterSetName = 'Path')]
    param(
        [Parameter(ParameterSetName = 'Path' , Position = 0, Mandatory = $true)]
        [Parameter(ParameterSetName = 'PassThru', Position = 0)]
        $Path,

        [Parameter(ParameterSetName = 'Path', Position = 1)]
        [Parameter(ParameterSetName = 'PassThru', Position = 1)]
        [OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png,

        [Parameter(ValueFromPipeline = $true)]
        [Alias("Driver")]
        [ValidateIsWebDriverAttribute()]
        $Target = $Global:SeDriver ,

        [Parameter(ParameterSetName = 'Base64', Mandatory = $true)]
        [Switch]$AsBase64EncodedString,

        [Parameter(ParameterSetName = 'PassThru', Mandatory = $true)]
        [Alias('PT')]
        [Switch]$PassThru
    )
    $Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($Target)
    if ($AsBase64EncodedString) { $Screenshot.AsBase64EncodedString }
    elseif ($Path) {
        $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
        $Screenshot.SaveAsFile($Path, $ImageFormat) 
    }
    if ($Passthru) { $Screenshot }
}
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

VBAを中心とした自動化、効率化の手法を紹介しています。現在は、SeleniumBasicのexamplesを紹介しています。その内、SeleniumBasic以外の手法も掲載したいと思っております。

目次