I am trying to grab one or more table cells that contain a particular string. I am not able to accomplish this with
var tableCells = table.FindElements(By.CssSelector("td:contains('my partial text')"));
What is the correct css selector text to use here?
I’ve also tried the following:
var tableCells = table.FindElements(By.TagName("td")).Where(tableCell => tableCell.Contains("my partial text"));
but it is extremely slow.
There is no such CSS selector as
:contains(). It was a proposal that was discarded years ago.The reason
table.FindElements(By.TagName("td")).Where(tableCell => tableCell.Contains("my partial text"));is slow should be at least partly obvious – you’re asking WebDriver to find every table cell in the document, and then iterate over them all.You can do this much more efficiently using an XPath locator, something like
table.FindElements(By.xpath("//td[contains(.,'my partial text')]")). This is exactly what it looks like – the XPath equivalent of your attempted CSS locator.