Running Selenium under Automation failing

I am using Selenium to get info from a webpage and when I run the script with powershell 7.2.1 and as the user I am using in powershell universal it runs fine. But in automation I get some wild errors.

$selRelativePath = "E:\PowerShellUniversal\UniversalAutomation\Repository\SeleniumTest\include"

#needed for selenium and edgedriver to be in the environment path.
$workingPath = (Get-Item -Path $selRelativePath).fullname
if (($env:Path -split ';') -notcontains $workingPath) {
    $env:Path += ";$workingPath"
}

Add-Type -Path "$($workingPath)\WebDriver.dll"

$edgeOptions = New-Object OpenQA.Selenium.Edge.EdgeOptions
$edgeOptions.AddArgument("--headless")
$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver($edgeOptions)

$EdgeDriver.Navigate().GoToUrl("https://www.ipchicken.com")

$pageData = $EdgeDriver.FindElement([OpenQA.Selenium.By]::TagName("body")).Text

$EdgeDriver.Quit()

Write-Output $pageData

Just for some simple setup you will need the Selenium DLL and Edge Driver

Selenium download, this will be a nuget package but I extracted with 7zip and the DLL is in lib\net5.0
https://www.nuget.org/api/v2/package/Selenium.WebDriver

Then the Edge Driver download copy the exe into the path folder with above DLL.
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Product: PowerShell Universal
Version: 2.9.2

Thanks,
Jeremy

What kind of errors are you seeing? And how are you hosting PSU?

So I updated my above code to take the environment path out of the equation. I am running PowerShell universal just as it installs as a service.

$selRelativePath = "E:\PowerShellUniversal\UniversalAutomation\Repository\SeleniumTest\include"

Add-Type -Path "$($selRelativePath)\WebDriver.dll"

$edgeOptions = New-Object OpenQA.Selenium.Edge.EdgeOptions
$edgeOptions.AddArgument("--headless")
$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver("$($selRelativePath)", $edgeOptions)

$EdgeDriver.Navigate().GoToUrl("https://www.ipchicken.com")

$pageData = $EdgeDriver.FindElement([OpenQA.Selenium.By]::TagName("body")).Text

$EdgeDriver.Quit()

Write-Output $pageData

This above runs outside of automation but not in it.

This is what I am seeing.

Mar 15, 2022 2:19 PM [error] Exception calling ".ctor" with "2" argument(s): "Cannot start the driver service on http://localhost:62359/" 
Mar 15, 2022 2:19 PM [error] You cannot call a method on a null-valued expression. 
Mar 15, 2022 2:19 PM [error] You cannot call a method on a null-valued expression. 
Mar 15, 2022 2:19 PM [error] You cannot call a method on a null-valued expression. 

Jeremy

Can you try enabling the interact with desktop option for the PSU service (if you haven’t already). It might have a hard time running a browser without that.

image

That made no difference but since it was running as local system I figured that might be a problem. So I change PSU to run under a service account and that solved it. I probably should have done that a while ago but that seems to be the ticket.

Thanks,
Jeremy