我正在更新以前使用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')))
使用Selenium4.0,FindElementById不再工作:
找不到OpenQA.Selenium.Support.UI.ExpectedConditions.类型
据我所知,OpenQA.Selenium.Support.UI.ExpectedConditions存在于WebDriver.Support中,对吗?
环顾四周寻找替代方案,我找到了SeleniumExtras.WaitHelpers,但这可能只适用于.netstandard2.1?
发布于 2021-12-08 18:47:32
最后,这对我起了作用:
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”。
发布于 2022-04-07 02:02:52
ExpectedConditions
类被弃用,并根据.Net变更量g删除
v3.11.0 标记为.NET ExpectedConditions过时。使用ExpectedConditions类与直接在代码中直接使用lambda函数(匿名方法)没有任何好处。由于社区似乎认为等待条件的“正式”存储库是可取的,所以现有代码已经迁移到基于GitHub的新组织下的新存储库中。希望这将鼓励来自社区的志愿者拥有这一守则。用户应该更新他们的引用并迁移他们的代码以使用
SeleniumExtras.ExpectedConditions
。这个实现将在以后的v4.0.0a1版本中从.NET语言绑定中删除 删除了不推荐的ExpectedConditions和PageFactory类,以及它们的支持类。
正如@StackExchangeGuy所指出的,在Powershell中使用脚本块类似于C#中的lamba表达式。要继续使用ExpectedConditions
类,您可以:( a)从源构建DotNetSeleniumExtras
并导入它或( b)降级到v3.141.0
。
https://stackoverflow.com/questions/70264992
复制相似问题