如何添加自定义ExpectedConditions的硒? [英] How to add custom ExpectedConditions for Selenium?

查看:927
本文介绍了如何添加自定义ExpectedConditions的硒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写我自己的硒ExpectedConditions,但我不知道如何添加一个新的。有没有人有一个例子吗?我找不到这个网上的任何教程。

I'm trying to write my own ExpectedConditions for Selenium but I don't know how to add a new one. Does anyone have an example? I can't find any tutorials for this online.

在我目前的情况下,我想等到一个元素存在,是可见的,已启用,并且不具备ATTR咏叹调禁用。我知道这个代码不工作:

In my current case I want to wait until an element exists, is visible, is enabled AND doesn't have the attr "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")
    }

编辑:有一点额外的信息:我遇到的问题是与jQuery的标签我有一个禁用选项卡上的形式,它会开始填充之前的标签被激活该选项卡上的字段。

A little additional info: the problem I am running into is with jQuery tabs. I have a form on a disabled tab and it will start filling out fields on that tab before the tab becomes active.

推荐答案

这是预期的条件只不过是一个匿名方法的详细使用lambda表达式。这些都成为自.NET 3.0 .NET开发的主食,尤其是与LINQ的释放。由于绝大多数.NET开发人员习惯使用C#的lambda语法,在.NET的webdriver绑定<$ C $ 。C> ExpectedConditions 实施只有几个方法

An "expected condition" is nothing more than an anonymous method using a lambda expression. These have become a staple of .NET development since .NET 3.0, especially with the release of 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.

创建等待像你问的会是这个样子:

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 = driver.FindElement(By.Id("myid"));
    if (element.Displayed &&
        element.Enabled &&
        element.GetAttribute("aria-disabled") == null)
    {
        return element;
    }

    return null;
});

如果你没有这个构造有经验,我会建议变得如此。只有可能成为.NET的未来版本中更为普遍。

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.

这篇关于如何添加自定义ExpectedConditions的硒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆