我试图从以下 HTML 页面中找到“x-tree-node”网络元素的列表。
<div class = "x-tree-root-node"> </div>
<li class = "x-tree-node"> </li>
<ul class = "x-tree-node-ct"> </ul>
<li class = "x-tree-node"> </li> #Starts from here, those elements are the list of 'x-tree-node' I want to find.
<li class = "x-tree-node"> </li> #I want this to be found in my list.
<li class = "x-tree-node"> </li> #I want this to be found in my list.
<li class = "x-tree-node"> </li> #I want this to be found in my list.
我使用的代码是:
public By TopLevelNodeLocator {
get { return By.CssSelector(".x-tree-root-node.x-tree-node.x-tree-node-ct.x-tree-node"); }
}
public IList<IWebElement> AllTopLevelNode {
get { return WebDriver.FindElements(TopLevelNodeLocator); }
}
但是我得到的IList返回了一个超时异常,也就是说什么都没有找到。
有人可以帮忙吗? 谢谢
最佳答案
By.CssSelector(".x-tree-node")
如果您只想要无序列表的子元素:
By.CssSelector(".x-tree-node-ct .x-tree-node")
或
By.CssSelector(".x-tree-node-ct > .x-tree-node")
或
By.CssSelector("ul .x-tree-node")
或
By.CssSelector("ul > .x-tree-node")
有很多方法可以做到这一点。
TopLevelNodeLocator 的属性名称听起来像是您在尝试定位根节点,不过,仅供引用。
关于c# - 如何在 C# 中使用 Css 选择器定位 Web 元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114573/