首页 > 解决方案 > 是否有可能如何使用 linq All、Any 和

问题描述

我正在使用 selenium 和 Specflow 在 C# 中创建测试。

我开始使用 linq,我想知道是否有一些简单的方法可以将 Any、All、Where 等和列表一起使用。我知道它返回 TRUE 或 False,但如果我能以某种方式将它与列表一起使用,那就太好了。

示例:有很多项目,其中只有一个没有 URL,所以在元素中我可以看到它没有 href,所以我通过 CssSelector 选择了元素并且有项目,正如我所说 - 其中一个没有网址

所以我创建了这个:

    public bool IsItemWithoutURL()
        {
            bool emptyUrlItems = items.Any(i => String.IsNullOrEmpty((i.GetAttribute("href"))));
            return emptyUrlItems;
        } 

这将返回我的真假,但我的目标是拿走那一件并与他合作。例如点击它等。

有什么办法吗?

标签: c#seleniumselenium-webdriver

解决方案


 public bool IsAnyItemWithoutURL()
 {
        var emptyItem = items.FirstOrDefault(i => String.IsNullOrEmpty((i.GetAttribute("href"))));

        //here you can do everything you want with emptyItem because it's not boolean.
        return emptyItem != null;
} 

推荐阅读