如何为Selenium添加自定义的ExpectedConditions?
我正在尝试为Selenium编写自己的ExpectedConditions,但我不知道如何添加新的。 有人有例子吗? 我在网上找不到任何教程。
在我目前的情况下,我想等到元素存在,可见,启用并且没有attr“aria-disabled”。 我知道这段代码不起作用:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds)); return wait.Until((d) => { return ExpectedConditions.ElementExists(locator) && ExpectedConditions.ElementIsVisible && d.FindElement(locator).Enabled && !d.FindElement(locator).GetAttribute("aria-disabled") }
编辑:一些额外的信息:我遇到的问题是使用jQuery选项卡。 我在禁用的选项卡上有一个表单,它将在选项卡变为活动状态之前开始填写该选项卡上的字段。
“预期条件”只不过是使用lambda表达式的匿名方法。 自.NET 3.0以来,这些已成为.NET开发的主要内容,尤其是LINQ的发布。 由于绝大多数.NET开发人员都熟悉C#lambda语法,因此WebDriver .NET绑定的ExpectedConditions
实现只有几个方法。
像你要求的那样创造一个等待看起来像这样:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until((d) => { IWebElement element = d.FindElement(By.Id("myid")); if (element.Displayed && element.Enabled && element.GetAttribute("aria-disabled") == null) { return element; } return null; });
如果你对这种结构没有经验,我会建议你这样做。 它很可能在未来的.NET版本中变得更加普遍。
我理解ExpectedConditions
背后的理论(我认为),但我经常发现它们很麻烦,难以在实践中使用。
我会采用这种方法:
public void WaitForElementPresentAndEnabled(By locator, int secondsToWait = 30) { new WebDriverWait(driver, new TimeSpan(0, 0, secondsToWait)) .Until(d => d.FindElement(locator).Enabled && d.FindElement(locator).Displayed && d.FindElement(locator).GetAttribute("aria-disabled") == null ); }
我很乐意从这里使用所有ExpectedConditions
的答案中学习:)
我已经将WebDriverWait和ExpectedCondition / s的示例从Java转换为C#。
Java版本:
WebElement table = (new WebDriverWait(driver, 20)) .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table#tabletable")));
C#版本:
上述就是C#学习教程:如何为Selenium添加自定义的ExpectedConditions?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
IWebElement table = new WebDriverWait(driver, TimeSpan.FromMilliseconds(20000)) .Until(ExpectedConditions.ElementExists(By.CssSelector("table#tabletable")));
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/958826.html