简体   繁体   English

如何为 Selenium 添加自定义 ExpectedConditions?

[英]How to add custom ExpectedConditions for Selenium?

I'm trying to write my own ExpectedConditions for Selenium but I don't know how to add a new one.我正在尝试为 Selenium 编写我自己的 ExpectedConditions,但我不知道如何添加一个新的。 Does anyone have an example?有人有例子吗? I can't find any tutorials for this online.我在网上找不到任何有关此的教程。

In my current case I want to wait until an element exists, is visible, is enabled AND doesn't have the attr "aria-disabled".在我目前的情况下,我想等到元素存在、可见、启用并且没有属性“aria-disabled”。 I know this code doesn't work:我知道此代码不起作用:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
return wait.Until<IWebElement>((d) =>
    {
        return ExpectedConditions.ElementExists(locator) 
        && ExpectedConditions.ElementIsVisible 
        &&  d.FindElement(locator).Enabled 
         && !d.FindElement(locator).GetAttribute("aria-disabled")
    }

EDIT: A little additional info: the problem I am running into is with jQuery tabs.编辑:一些附加信息:我遇到的问题是 jQuery 选项卡。 I have a form on a disabled tab and it will start filling out fields on that tab before the tab becomes active.我在禁用的选项卡上有一个表单,它会在选项卡变为活动状态之前开始填写该选项卡上的字段。

An "expected condition" is nothing more than an anonymous method using a lambda expression. “预期条件”只不过是使用 lambda 表达式的匿名方法。 These have become a staple of .NET development since .NET 3.0, especially with the release of LINQ.自 .NET 3.0 以来,这些已成为 .NET 开发的主要内容,尤其是随着 LINQ 的发布。 Since the vast majority of .NET developers are comfortable with the C# lambda syntax, the WebDriver .NET bindings' ExpectedConditions implementation only has a few methods.由于绝大多数 .NET 开发人员都熟悉 C# lambda 语法,因此 WebDriver .NET 绑定的ExpectedConditions实现只有几个方法。

Creating a wait like you're asking for would look something like this:像您要求的那样创建等待看起来像这样:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until<IWebElement>((d) =>
{
    IWebElement element = d.FindElement(By.Id("myid"));
    if (element.Displayed &&
        element.Enabled &&
        element.GetAttribute("aria-disabled") == null)
    {
        return element;
    }

    return null;
});

If you're not experienced with this construct, I would recommend becoming so.如果您对这种结构没有经验,我建议您这样做。 It is only likely to become more prevalent in future versions of .NET.它只会在 .NET 的未来版本中变得更加流行。

I understand the theory behind ExpectedConditions (I think), but I often find them cumbersome and difficult to use in practice.我理解ExpectedConditions背后的理论(我认为),但我经常发现它们在实践中很麻烦且难以使用。

I would go with this sort of approach:我会采用这种方法:

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

I will be happy to learn from an answer that uses all ExpectedConditions here :)我很乐意从这里使用所有ExpectedConditions的答案中学习:)

Since all of these answers point the OP to use a separate method with a new wait and encapsulate the function instead of actually using a custom expected conditions, I'll post my answer:由于所有这些答案都指向 OP 使用带有新等待的单独方法并封装函数而不是实际使用自定义预期条件,因此我将发布我的答案:

  1. Create a class CustomExpectedConditions.cs创建一个类 CustomExpectedConditions.cs
  2. Create each one of your conditions as static accessible methods that you can later call from your wait将您的每一个条件创建为静态可访问方法,您以后可以从等待中调用这些方法
public class CustomExpectedConditions { public static Func<IWebDriver, IWebElement> ElementExistsIsVisibleIsEnabledNoAttribute(By locator) { return (driver) => { IWebElement element = driver.FindElement(locator); if (element.Displayed && element.Enabled && element.GetAttribute("aria-disabled").Equals(null)) { return element; } return null; }; } }

Now you can call it as you would any other expected condition like so:现在您可以像任何其他预期条件一样调用它,如下所示:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIMEOUT));
wait.Until(CustomExpectedConditions.ElementExistsIsVisibleIsEnabledNoAttribute(By.Id("someId")));

I've converted an example of WebDriverWait and ExpectedCondition/s from Java to C#.我已将 WebDriverWait 和 ExpectedCondition/s 的示例从 Java 转换为 C#。

Java version:爪哇版:

WebElement table = (new WebDriverWait(driver, 20))  
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table#tabletable")));

C# version: C#版本:

IWebElement table = new WebDriverWait(driver, TimeSpan.FromMilliseconds(20000))
.Until(ExpectedConditions.ElementExists(By.CssSelector("table#tabletable")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在页面对象模型设计中如何使用硒ExpectedConditions? - How do you use selenium ExpectedConditions in a page object model design? C# Selenium &#39;ExpectedConditions 已过时&#39; - C# Selenium 'ExpectedConditions is obsolete' Selenium C# 使用 IWebElement 的预期条件 - Selenium C# ExpectedConditions with IWebElement 如何在不使用已弃用的 ExpectedConditions 的情况下在 selenium 测试中遇到诸如“元素不可交互”异常之类的异常 - How to encounter exceptions like “element not interactable” exception in selenium test without use of ExpectedConditions which are deprecated 硒-预期条件的扩展方法无法识别,或者:更好的替代方法? - Selenium - Extension method for ExpectedConditions not recognized, or: better alternatives? 如何在C#中实现ExpectedConditions.AlertIsPresent - How to implement ExpectedConditions.AlertIsPresent in C# Selenium C#wait.until(expectedconditions)…函数无法在屏幕上找到对象/元素 - Selenium c# wait.until(expectedconditions)… function fails to find objects/elements on screen 如何添加自定义列 - How to add a custom column 如何将Selenium中的IWebElement添加到C#上的List &lt;&gt;()? - How to add IWebElement in Selenium to List<>() on C#? SeleniumExtras.WaitHelpers.ExpectedConditions - SeleniumExtras.WaitHelpers.ExpectedConditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM