Matt W
Matt W

Reputation: 12434

Selenium CSS selector throwing OpenQA.Selenium.InvalidSelectorException - what's wrong?

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]

The exception being thrown is:

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?

I have tried simplifying it to:

//table[@id='prepaymentItemsDataGrid']

which has no effect.

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?

Upvotes: 0

Views: 394

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193338

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]");
    

Upvotes: 4

Related Questions