Is there a CSS selector for elements containing certain text?

后端 未结 18 2361
别跟我提以往
别跟我提以往 2020-11-21 04:26

I am looking for a CSS selector for the following table:

Peter    | male    | 34
Susanne  | female  | 12

Is there any selector to match all

18条回答
  •  孤街浪徒
    2020-11-21 05:04

    For those who are looking to do Selenium CSS text selections, this script might be of some use.

    The trick is to select the parent of the element that you are looking for, and then search for the child that has the text:

    public static IWebElement FindByText(this IWebDriver driver, string text)
    {
        var list = driver.FindElement(By.CssSelector("#RiskAddressList"));
        var element = ((IJavaScriptExecutor)driver).ExecuteScript(string.Format(" var x = $(arguments[0]).find(\":contains('{0}')\"); return x;", text), list);
        return ((System.Collections.ObjectModel.ReadOnlyCollection)element)[0];
    }
    

    This will return the first element if there is more than one since it's always one element, in my case.

提交回复
热议问题