Selenium CSS选择器抛出OpenQA.Selenium.InvalidSelectorException-怎么了? [英] Selenium CSS selector throwing OpenQA.Selenium.InvalidSelectorException - what's wrong?

查看:74
本文介绍了Selenium CSS选择器抛出OpenQA.Selenium.InvalidSelectorException-怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium中的CSS选择器查找表格单元.这是我的选择器:

I am attempting to find a table cell using a CSS selector in Selenium. This is my selector:

//table[@id='prepaymentItemsDataGrid']//tr[@class='tablerowlight']/td[3]

抛出的异常是:

Driver.FindElementByCssSelector("//table[@id='prepaymentItemsDataGrid']//tr[@class='tablerowlight']/td[3]") threw an exception of type 'OpenQA.Selenium.InvalidSelectorException'

我的选择器出了什么问题?我怎么知道硒在抱怨什么?

What is wrong with my selector? How do I know what Selenium is complaining about?

我尝试将其简化为:

//table[@id='prepaymentItemsDataGrid']

无效.

Selenium是否有不同的执行CSS选择器的方法,如何在尝试运行选择器之前检查其是否有效?

Does Selenium have a different method of executing CSS selectors and how can I check that the selector is valid before trying to run it?

推荐答案

尽管您尝试使用 FindElementByCssSelector(),但却是 XPath .您可以使用以下 Locator Strategies 定位器策略:

Though you have tried to use FindElementByCssSelector() but the values were of XPath. You can use either of the following Locator Strategies :

  • CssSelector :

Driver.FindElementByCssSelector("table#prepaymentItemsDataGrid tr.tablerowlight > td:nth-of-type(3)");

  • XPath :

    Driver.FindElementXPath("//table[@id='prepaymentItemsDataGrid']//tr[@class='tablerowlight']/td[3]");
    

  • 这篇关于Selenium CSS选择器抛出OpenQA.Selenium.InvalidSelectorException-怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆