1

我正在更新一些以前使用 Selenium 3.141 的 PowerShell 代码。我有以下代码片段:

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')))

在 Selenium 4.0 中,FindElementById 不再起作用:

找不到类型 [OpenQA.Selenium.Support.UI.ExpectedConditions]。

据我所知,OpenQA.Selenium.Support.UI.ExpectedConditions 存在于 WebDriver.Support 中,对吧?

环顾四周寻找替代品,我找到了 SeleniumExtras.WaitHelpers,但这可能只适用于 .netstandard2.1?

4

1 回答 1

2

最后,这对我有用:

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 } })

如果要返回元素对象而不是布尔值,只需将“System.Boolean”(在最后一行)更改为“OpenQA.Selenium.IWebElement”。

于 2021-12-08T18:47:32.783 回答