2

I am working on updating some PowerShell code that previously worked with Selenium 3.141. I have the following code snippet:

Add-Type -LiteralPath "$seleniumPath\lib\net48\WebDriver.dll"
Add-Type -LiteralPath "$seleniumPath\lib\net48\WebDriver.Support.dll"
$url = "https://<webpage.com>"
$options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$options.AddArgument("--disable-gpu")
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options)
[OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(60))

$driver.Navigate().GoToURL($url)
$driver.FindElementById("username")
$wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists([OpenQA.Selenium.By]::Id('username')))

With Selenium 4.0, FindElementById no longer works:

Unable to find type [OpenQA.Selenium.Support.UI.ExpectedConditions].

As far as I can tell, OpenQA.Selenium.Support.UI.ExpectedConditions exists in WebDriver.Support, right?

Looking around for alternatives, I found SeleniumExtras.WaitHelpers, but that maybe only works with .netstandard2.1?

StackExchangeGuy
  • 741
  • 16
  • 36
  • Maybe check here: https://stackoverflow.com/questions/49866334/c-sharp-selenium-expectedconditions-is-obsolete – Captain_Planet Dec 08 '21 at 09:14
  • Yeah, I saw that. Looking closer, if I try to do the same thing with PowerShell, I get an error. ```$wait.Until((Invoke-Command -ScriptBlock { param([OpenQA.Selenium.IWebDriver]$dummy) [OpenQA.Selenium.IWebElement]$element = $global:driver.FindElement([OpenQA.Selenium.By]::Id("username")); return $element}))``` results in "Cannot find an overload for "Until" and the argument count: "1"." But ```$global:driver.FindElement([OpenQA.Selenium.By]::Id("username"))``` does return the element (once the page is loaded). – StackExchangeGuy Dec 08 '21 at 17:48

2 Answers2

5

In the end, this worked for me:

Add-Type -LiteralPath "$seleniumPath\lib\net48\WebDriver.dll"
Add-Type -LiteralPath "$seleniumPath\lib\net48\WebDriver.Support.dll"
$url = "https://<webpage.com>"
$options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$options.AddArgument("--disable-gpu")
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options)
[OpenQA.Selenium.Support.UI.WebDriverWait]$wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait ($driver, [System.TimeSpan]::FromSeconds(60))

$driver.Navigate().GoToURL($url)
$driver.FindElementById("username")

$wait.Until([System.Func[OpenQA.Selenium.IWebDriver, System.Boolean]] { param($driver) Try { $driver.FindElement([OpenQA.Selenium.By]::Id('username')) } Catch { $null } })

If you want to return the element object instead of a boolean value, you can just change "System.Boolean" (on the last line) to "OpenQA.Selenium.IWebElement".

StackExchangeGuy
  • 741
  • 16
  • 36
0

The ExpectedConditions class was deprecated and removed according to the .Net changelog:

v3.11.0

Marked .NET ExpectedConditions obsolete. Using the ExpectedConditions class provides no benefit over directly using lambda functions (anonymous methods) directly in one's code. Since the community appears to believe that an "official" repository of wait conditions is desireable, the existing code has been migrated to a new repository under a new organization on GitHub. It is hoped that this will encourage a volunteer from the community to take ownership of this code. Users should update their references and migrate their code to use SeleniumExtras.ExpectedConditions. This implementation will be removed from the .NET language bindings in a future release

v4.0.0a1

Removed deprecated ExpectedConditions and PageFactory classes, as well as the supporting classes thereof.

Using scriptblocks in Powershell is similar to lamba expressions in C# as @StackExchangeGuy pointed out. To continue to use the ExpectedConditions class you could a) build DotNetSeleniumExtras from source and import it or b) downgrade to v3.141.0.