Allow specifying any kind of button selector

This commit is contained in:
Manuel Thalmann 2024-09-26 04:09:36 +02:00
parent 3902fd7184
commit 05afb80c6d

View file

@ -2,6 +2,19 @@
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/System.ps1"; . "$PSScriptRoot/System.ps1";
<#
.SYNOPSIS
Imports the resources for using browser automation.
#>
function Import-BrowserAutomation {
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) {
$packageRoot = Split-Path -Parent (Get-Package Selenium.WebDriver).Source;
$file = Join-Path $packageRoot "lib/netstandard2.0/WebDriver.dll";
$env:SE_MANAGER_PATH = Join-Path $packageRoot "manager" ($IsWindows ? "windows" : "linux") "selenium-manager$($IsWindows ? ".exe" : '')";
$null = [System.Reflection.Assembly]::LoadFile($file);
}
}
<# <#
.SYNOPSIS .SYNOPSIS
Runs an action involving browser automation. Runs an action involving browser automation.
@ -14,13 +27,7 @@ $null = New-Module {
[scriptblock] $Action [scriptblock] $Action
) )
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) { Import-BrowserAutomation;
$packageRoot = Split-Path -Parent (Get-Package Selenium.WebDriver).Source;
$file = Join-Path $packageRoot "lib/netstandard2.0/WebDriver.dll";
$env:SE_MANAGER_PATH = Join-Path $packageRoot "manager" ($IsWindows ? "windows" : "linux") "selenium-manager$($IsWindows ? ".exe" : '')";
$null = [System.Reflection.Assembly]::LoadFile($file);
}
& $Action; & $Action;
} }
@ -102,7 +109,7 @@ $null = New-Module {
$result; $result;
} }
catch { catch {
Write-Error "$Error"; Write-Error $_;
} }
} }
} }
@ -126,7 +133,7 @@ $null = New-Module {
function Start-BrowserDownload { function Start-BrowserDownload {
param( param(
[string] $URL, [string] $URL,
[string] $ButtonSelector, $ButtonSelector,
[string] $OutDir = $null, [string] $OutDir = $null,
[double] $Timeout = 0 [double] $Timeout = 0
) )
@ -136,7 +143,12 @@ $null = New-Module {
[OpenQA.Selenium.Firefox.FirefoxDriver] $Browser [OpenQA.Selenium.Firefox.FirefoxDriver] $Browser
) )
$selector = [OpenQA.Selenium.By]::CssSelector($ButtonSelector); if ($ButtonSelector -is [string]) {
$selector = [OpenQA.Selenium.By]::CssSelector($ButtonSelector);
} else {
$selector = $ButtonSelector;
}
[OpenQA.Selenium.IWebElement] $element = $null; [OpenQA.Selenium.IWebElement] $element = $null;
for ($i = 0; $i -lt 5; $i++) { for ($i = 0; $i -lt 5; $i++) {