目次
概要
起動しているブラウザのクッキー情報を追加するメソッドになります。
PowerShellでSeleniumモジュールを利用する方法
Seleniumは多言語で利用可能なツールです。PowerShell専用でSeleniumのクラスは用意されていませんが、C#用に.NETのSeleniumは用意されています。公式には例文も無い...
コマンド(Commond)
Set-SeCookie
代替コマンド(Alias)
無し
構文(Syntax)
Set-SeCookie [[-Name] <string>] [[-Value] <string>] [[-Path] <string>] [[-Domain] <string>] [[-ExpiryDate] <Object>] [[-Target] <Object>] [<CommonParameters>]
オプション
Name | 引数型 | 解説 | Short Example |
---|---|---|---|
Name | string | Name値指定 | Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver |
Value | string | Value値指定 | Set-SeCookie -Name "hoge" -Value "hogehoge" -Target $Driver |
Path | string | Path値指定 | |
Domain | string | Domain値指定 | |
ExpiryDate | Object | ExpiryDate値指定 | |
Target | Object | 対象ドライバーの指定 | 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)
}
}