Dav*_*her 50 c# selenium webdriver
当尝试使用ExpectedConditions显式等待元素变得可见时,Visual Studio警告我它现在已经过时,很快就会从Selenium中删除.
实现相同结果的当前/新方法是什么?
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section")));
Run Code Online (Sandbox Code Playgroud)
Dav*_*her 87
我解决了自己的问题,想为其他人提供答案,想知道如何使用最新版本的Selenium来解决这个问题.
使用nuget,搜索DotNetSeleniumExtras.WaitHelpers,将该命名空间导入您的类.现在你可以这样做:
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("content-section")));
Run Code Online (Sandbox Code Playgroud)
IDE中的警告将消失.
小智 14
如果您不想下载额外的nuget包,则很容易声明自己的函数(或条件),尤其是使用lamda表达式,例如
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(condition =>
{
try
{
var elementToBeDisplayed = driver.FindElement(By.Id("content-section"));
return elementToBeDisplayed.Displayed;
}
catch (StaleElementReferenceException)
{
return false;
}
catch (NoSuchElementException)
{
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
这也是非常多才多艺,因为现在可以评估任何类型的bool表达式.
小智 12
这很简单,只是改变
Wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section")));
Run Code Online (Sandbox Code Playgroud)
至
Wait.Until(c => c.FindElement(By.Id("content-section")));
Run Code Online (Sandbox Code Playgroud)
Jam*_*e F 10
基于Rob F. 的回答,我在我的项目中添加了扩展方法。(其实我加了两个,WaitUntilVisible(...)
和WaitUntilClickable(...)
。)
这些返回元素,而不是布尔值,所以它更像是 Wait.Until(ExpectedConditions...)
// use: element = driver.WaitUntilVisible(By.XPath("//input[@value='Save']"));
public static IWebElement WaitUntilVisible(
this IWebDriver driver,
By itemSpecifier,
int secondsTimeout = 10)
{
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, secondsTimeout));
var element = wait.Until<IWebElement>(driver =>
{
try
{
var elementToBeDisplayed = driver.FindElement(itemSpecifier);
if(elementToBeDisplayed.Displayed)
{
return elementToBeDisplayed;
}
return null;
}
catch (StaleElementReferenceException)
{
return null;
}
catch (NoSuchElementException)
{
return null;
}
});
return element;
}
Run Code Online (Sandbox Code Playgroud)
NuGet 是必需的 - DotNetSeleniumExtras.WaitHelpers
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("")));
Run Code Online (Sandbox Code Playgroud)
我刚刚演示了元素可点击事件。同样,其他事件可以与所需的参数一起使用。
小智 5
The answers to change to anonymous function is the most correct one. Or write your own class of your own, needed, wait conditions. An example of using an anonymous function for the explicit scenario above would be something like...
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
var element = wait.Until(() =>
{
var e = Driver.FindElement(By.Id("content-section"));
if(e.Displayed)
return e;
});
Run Code Online (Sandbox Code Playgroud)
And at that point, the function itself could be off on its own in some class in your solution that you can call. The nice thing with this is that you can modify as needed; I have seen several cases where really poorly made websites end up breaking how the ExpectedConditions work, and that was solved with the team writing our own function.
As per the C# contributor:
With respect to ExpectedConditions, again, this was an addition that was created in .NET solely because "Java has it." At the time the ExpectedConditions class in Java was created, the syntax for creating a lambda function (or something that acted like one) was particularly arcane and difficult to understand. In that case, a helper class made lots of sense for the Java bindings. However, C# isn't Java. In C#, the syntax for creating lambda functions ("anonymous methods" in the language of Microsoft's documentation) has been well understood by C# developers for many years, and is a standard tool in their arsenal.
In this case, the question of code verbosity does have some merit, but since wait conditions are rarely one-size-fits-all, it would be a much cleaner approach for users to develop their own conditions class that has the wait conditions they're interested in. This, however, is something users have an aversion to. Additionally, the thought of a 'standard' collection of implementations of specific wait conditions seems to be a good idea on its face, but there is a great deal of variation on the way users want any given condition to work. Having a collection of wait conditions might be a good thing, but the Selenium project is not the place for it.
http://jimevansmusic.blogspot.com/2018/03/deprecating-parts-of-seleniums-net.html
以下 C# 代码对我有用:
new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(c => c.FindElement(By.Id("name")));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35101 次 |
最近记录: |