我有这个属性:
public IEnumerable<Drawing> Drawings {
get {
return Root.FindComponents<Drawing>(By.CssSelector("tbody tr[role='row']"));
}
}
调用这个扩展方法:
public static IEnumerable<T> FindComponents<T>(this IWebElement element, By by) where T : PageComponent, new() {
return element.FindElements(by).Select((el) => {
T t = new T();
t.Root = el;
return t;
});
}
当我查看 Visual Studio 的监 window 口时,我的选择器已被截断为 tbody
之后的部分,现在只显示 tr[role='row']
并且它在监 window 口中以红色突出显示。
这最终会选择一些错误的元素。
这是怎么回事?为什么我不能使用这个完全有效的选择器?当我使用 document.querySelectorAll
时,选择器在目标浏览器 (FireFox) 中工作。
这是 html 结构:
(表格是根元素)
<table role="grid">
<colgroup>
<col style="width:35px" />
<col style="width:35px" />
<col style="width:35px" />
<col style="width:35px" />
<col style="width:120px" />
<col style="width:35px" />
<col style="width:35px" />
<col style="width:40px" />
</colgroup>
<tbody role="rowgroup">
<tr role="row">
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
<td role="gridcell"></td>
</tr>
<!-- ... etc (repeats like the above) -->
</tbody>
</table>
最佳答案
不确定这是否有帮助...
找到此资源 https://saucelabs.com/resources/selenium/css-selectors
这会建议像这样替换您的选择器:
tbody tr[@role='row']
或
tbody>tr[@role='row']
不确定两者是否会更好,但我想值得一试。
注意:我注意到它已经被推荐使用 >
但据我所知不要与 @role
结合使用。
关于c# - CSS 选择器不工作——被 Selenium WebDriver 截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776773/