Is there a CSS selector for elements containing certain text?

后端 未结 18 2285
别跟我提以往
别跟我提以往 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 04:57

    Most of the answers here try to offer alternative to how to write the HTML code to include more data because at least up to CSS3 you cannot select an element by partial inner text. But it can be done, you just need to add a bit of vanilla JavaScript, notice since female also contains male it will be selected:

          cells = document.querySelectorAll('td');
        	console.log(cells);
          [].forEach.call(cells, function (el) {
        	if(el.innerText.indexOf("male") !== -1){
        	//el.click(); click or any other option
        	console.log(el)
        	}
        });
     <table>
          <tr>
            <td>Peter</td>
            <td>male</td>
            <td>34</td>
          </tr>
          <tr>
            <td>Susanne</td>
            <td>female</td>
            <td>14</td>
          </tr>
        </table>

    <table>
      <tr>
        <td data-content="Peter">Peter</td>
        <td data-content="male">male</td>
        <td data-content="34">34</td>
      </tr>
      <tr>
        <td data-conten="Susanne">Susanne</td>
        <td data-content="female">female</td>
        <td data-content="14">14</td>
      </tr>
    </table>
    
    0 讨论(0)
  • 2020-11-21 04:59

    If I read the specification correctly, no.

    You can match on an element, the name of an attribute in the element, and the value of a named attribute in an element. I don't see anything for matching content within an element, though.

    0 讨论(0)
  • 2020-11-21 05:02

    You could set content as data attribute and then use attribute selectors, as shown here:

    /* Select every cell containing word "male" */
    td[data-content="male"] {
      color: red;
    }
    
    /* Select every cell starting on "p" case insensitive */
    td[data-content^="p" i] {
      color: blue;
    }
    
    /* Select every cell containing "4" */
    td[data-content*="4"] {
      color: green;
    }
    <table>
      <tr>
        <td data-content="Peter">Peter</td>
        <td data-content="male">male</td>
        <td data-content="34">34</td>
      </tr>
      <tr>
        <td data-content="Susanne">Susanne</td>
        <td data-content="female">female</td>
        <td data-content="14">14</td>
      </tr>
    </table>

    You can also use jQuery to easily set the data-content attributes:

    $(function(){
      $("td").each(function(){
        var $this = $(this);
        $this.attr("data-content", $this.text());
      });
    });
    
    0 讨论(0)
  • 2020-11-21 05:03

    There is actually a very conceptual basis for why this hasn't been implemented. It is a combination of basically 3 aspects:

    1. The text content of an element is effectively a child of that element
    2. You cannot target the text content directly
    3. CSS does not allow for ascension with selectors

    These 3 together mean that by the time you have the text content you cannot ascend back to the containing element, and you cannot style the present text. This is likely significant as descending only allows for a singular tracking of context and SAX style parsing. Ascending or other selectors involving other axes introduce the need for more complex traversal or similar solutions that would greatly complicate the application of CSS to the DOM.

    0 讨论(0)
  • 2020-11-21 05:03

    If you're using Chimp / Webdriver.io, they support a lot more CSS selectors than the CSS spec.

    This, for example, will click on the first anchor that contains the words "Bad bear":

    browser.click("a*=Bad Bear");
    
    0 讨论(0)
  • 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<IWebElement>)element)[0];
    }
    

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

    0 讨论(0)
提交回复
热议问题