c# - CSS 选择器不工作——被 Selenium WebDriver 截断

标签 c# selenium-webdriver

我有这个属性:

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/

相关文章:

c# - 用于学习目的的复杂 C#

c# - 不正确的值被放入 C# 中的列表中

c# - 如何使用带有 C# 的 Selenium 选择 img 标签标题或带有 td 的 alt

c# - .NET5辅助服务模板的NLog设置

c# - MySQL Connector/ODBC 5.2.2 中服务器端准备语句的问题

c# - 在 UWP 应用图像控件、桌面和手机中显示 SVG

python - Selenium:在 find_element_by_partial_link_text() 中使用多个字符串

selenium-webdriver - 为什么 data-* 属性比普通 ID 属性更适合元素选择?

python - 通过python上的selenium headless chrome下载文件

javascript - 通过 selenium Web 驱动程序选择文件后无法验证文件输入元素