2

I am trying to get "text that I want" from the site with this structure of code:

 <td class="x">
    <h3 class="x"> number </h3>
        <p>
            <a href="#"> text that I want</a>;
        </p>
</td>

If there will be one td with class "x" then I will do this:

$('td.x > p > a').text() 

and get text that I want, but the problem is that on this site there are a lot of "td" and "h3" elements with the same class "x". The only difference is that each time the text that is in "h3" element is a different number and I know what number is in "h3" element on the place where is my link. For example:

  <td class="x">
        <h3 class="x"> **125** </h3>
            <p>
                <a href="#"> text that I want</a>;
            </p>
    </td>

The question is - is it possible to choose selector based on the text that is inside - in my example I know that in code there is h3 element with text "125" or maybe is better way to get text from "a" element in my case.

wlo89
  • 21
  • 1
  • 2
  • Does this answer your question? [Cheerio: How to select element by text content?](https://stackoverflow.com/questions/34709765/cheerio-how-to-select-element-by-text-content) – ggorlen Oct 27 '22 at 22:58

1 Answers1

2

Contains is the selector you're looking for

$('h3:contains("**125**")')

This will select h3 that has the text you wanted

Gene Sy
  • 1,325
  • 2
  • 10
  • 17