【PowerShell】Selenium-Module Set-SeCookie クッキー情報の追加

目次

概要

 起動しているブラウザのクッキー情報を追加するメソッドになります。

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

コマンド(Commond)

Set-SeCookie

代替コマンド(Alias)

無し

構文(Syntax)

Set-SeCookie [[-Name] <string>] [[-Value] <string>] [[-Path] <string>] [[-Domain] <string>] [[-ExpiryDate] <Object>] [[-Target] <Object>] [<CommonParameters>]

オプション

Name引数型解説Short Example
NamestringName値指定Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver
ValuestringValue値指定Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver
PathstringPath値指定
DomainstringDomain値指定
ExpiryDateObjectExpiryDate値指定
TargetObject対象ドライバーの指定Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver
CommonParameters共通パラメータ

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

Example

$Driver = Start-SeChrome -Quiet
Open-SeUrl https://programan.org/ -Target $Driver
$cookie = Get-SeCookie -Target $Driver
Write-Host $cookie.Count
Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver

$cookie = Get-SeCookie -Target $Driver
Write-Host $cookie.Count
$cookie
Start-Sleep -Seconds 2
Stop-SeDriver $Driver

Module

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

function Set-SeCookie {
    [cmdletbinding()]
    param(
        [string]$Name,
        [string]$Value,
        [string]$Path,
        [string]$Domain,
        $ExpiryDate,

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

    <# Selenium Cookie Information
    Cookie(String, String)
    Initializes a new instance of the Cookie class with a specific name and value.
    Cookie(String, String, String)
    Initializes a new instance of the Cookie class with a specific name, value, and path.
    Cookie(String, String, String, Nullable<DateTime>)
    Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
    Cookie(String, String, String, String, Nullable<DateTime>)
    Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
    #>
    
    begin {
        if ($null -ne $ExpiryDate -and $ExpiryDate.GetType().Name -ne 'DateTime') {
            throw '$ExpiryDate can only be $null or TypeName: System.DateTime'
        }
    }

    process {
        if ($Name -and $Value -and (!$Path -and !$Domain -and !$ExpiryDate)) {
            $cookie = [OpenQA.Selenium.Cookie]::new($Name, $Value)
        }
        Elseif ($Name -and $Value -and $Path -and (!$Domain -and !$ExpiryDate)) {
            $cookie = [OpenQA.Selenium.Cookie]::new($Name, $Value, $Path)
        }
        Elseif ($Name -and $Value -and $Path -and $ExpiryDate -and !$Domain) {
            $cookie = [OpenQA.Selenium.Cookie]::new($Name, $Value, $Path, $ExpiryDate)
        }
        Elseif ($Name -and $Value -and $Path -and $Domain -and (!$ExpiryDate -or $ExpiryDate)) {
            if ($Target.Url -match $Domain) {
                $cookie = [OpenQA.Selenium.Cookie]::new($Name, $Value, $Domain, $Path, $ExpiryDate)
            }
            else {
                Throw 'In order to set the cookie the browser needs to be on the cookie domain URL'
            }
        }
        else {
            Throw "Incorrect Cookie Layout:
            Cookie(String, String)
            Initializes a new instance of the Cookie class with a specific name and value.
            Cookie(String, String, String)
            Initializes a new instance of the Cookie class with a specific name, value, and path.
            Cookie(String, String, String, Nullable<DateTime>)
            Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
            Cookie(String, String, String, String, Nullable<DateTime>)
            Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
        }

        $Target.Manage().Cookies.AddCookie($cookie)
    }
}
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

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

目次