From 51f352f0956791a53e1d9dd9b39cc5ebabd8f68d Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 14 Aug 2024 18:47:29 +0200 Subject: [PATCH] Improve handling of slow websites --- scripts/Common/Scripts/BrowserAutomation.ps1 | 45 +++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/scripts/Common/Scripts/BrowserAutomation.ps1 b/scripts/Common/Scripts/BrowserAutomation.ps1 index 1fc815fa..1ea3f272 100644 --- a/scripts/Common/Scripts/BrowserAutomation.ps1 +++ b/scripts/Common/Scripts/BrowserAutomation.ps1 @@ -90,18 +90,24 @@ $null = New-Module { $browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); $browser.Navigate().GoToUrl($URL); - $null = & $downloadAction -Browser $browser; - while (& $downloadChecker) { - Write-Host "Waiting for the download to finish…"; - Start-Sleep 1; + try { + $null = & $downloadAction -Browser $browser; + + while (& $downloadChecker) { + Write-Host "Waiting for the download to finish…"; + Start-Sleep 1; + } + + $file = Get-ChildItem $dir; + $result = Move-Item $file $OutDir -PassThru; + $browser.Quit(); + Remove-Item -Recurse $dir; + $result; + } + catch { + throw $Error; } - - $file = Get-ChildItem $dir; - $result = Move-Item $file $OutDir -PassThru; - $browser.Quit(); - Remove-Item -Recurse $dir; - $result; } } @@ -130,7 +136,24 @@ $null = New-Module { [OpenQA.Selenium.Firefox.FirefoxDriver] $Browser ) - $Browser.FindElement([OpenQA.Selenium.By]::CssSelector($ButtonSelector)).Click(); + $selector = [OpenQA.Selenium.By]::CssSelector($ButtonSelector); + [OpenQA.Selenium.IWebElement] $element = $null; + + for ($i = 0; $i -lt 5; $i++) { + $element = $Browser.FindElement($selector); + + if ($element) { + break; + } else { + Start-Sleep 1; + } + } + + if ($element) { + $Browser.FindElement([OpenQA.Selenium.By]::CssSelector($ButtonSelector)).Click(); + } else { + throw "Unable to find download button!"; + } }; } };