c# - 是否有可能如何使用 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;
}
这将返回我的真假,但我的目标是拿走那一件并与他合作。例如点击它等。
有什么办法吗?
解决方案
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;
}
推荐阅读
- python-3.x - Python脚本从文本文件grep字段并将输出写入csv文件
- r - 在 df 的两个变量中找到第一次出现
- typo3 - 如何为 TYPO3 9.5 返工 TYPO3 8 AbstractViewHelper
- git - git分支规则
- facebook-graph-api - 如何以编程方式获取拆分测试的结果?
- javascript - JSX 元素类型“Line”不是 JSX 元素的构造函数
- android - 如何从 initstate 中删除小部件或仅在 initstate 中加载小部件小部件一次?
- google-cloud-platform - RuntimeError:构建自定义图像时出错
- python - 如何在 python 脚本中保护纯文本密码
- jquery - 如何使用 settimeout 在 jquery 中制作 Preloader?