我一直在学习 LINQ,并尝试将其应用到使用 selenium 查找页面上的某些 Web 元素。
我有一个角度站点,它显示一个由 div 组成的表格。一列中的所有行,包括标题,都具有相同的属性(例如 css 选择器/类名等)。
我想找到一列中所有可见的输入框,并想出了这个...
var applyBoxes2 = from box in driver.FindElements(By.CssSelector("div.col-apply.col-md-1"))
where box.Text != "Apply?"
select box;
var clickableBox = from box in applyBoxes2
where box.FindElement(By.TagName("input")).Displayed == true
select box.FindElement(By.TagName("input"));
第一个查询是必需的,因为第一行是标题,不包含输入字段。如果我没有这个查询,我的第二个查询将在尝试执行 findelement = "input"时失败。
有谁知道我是否可以合并这两个陈述?所以首先我会过滤掉标题,然后我会在每个 div 中找到每个输入框。
任何关于如何改进的想法都将受到欢迎。
非常感谢,
最佳答案
使用&&
操作符,第一条语句没有通过就短路。因此,在您的情况下,如果文本为 Apply?
,它不会尝试查找元素。
var clickableBox = from box in driver.FindElements(By.CssSelector("div.col-apply.col-md-1"))
where box.Text != "Apply?" && box.FindElement(By.TagName("input")).Displayed == true
select box.FindElement(By.TagName("input"));
关于c# - 如何使用 LINQ 在 C# 中查找嵌套的 selenium WebElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42627158/