Home > Blockchain >  Match on certain text and go to the next h1 element using xpath
Match on certain text and go to the next h1 element using xpath

Time:10-18

I want to get the text inside the element that comes after a certain text ie. 'Projected earning growth'. I want to get the value 18.10% . The pattern I want to follow is lookup for a certain text as above, and then get the text following the immediate h2 element.

Is it possible to have one xpath expression that will do both the steps together?

  1. Match the div for a certain text 'Projected earning growth'
  2. Go to the immediate h1 element outside the div (I have to jump two divs here to get to the h1 element)

How do I match for a text and get to the next immediate h1 element via xpath?

<div>
    <div class="p small mb-8 box-label text-left text-blue-500">
        <div class="tt-container">
            <button aria-label="open tooltip" class="button link" type="button" role="tooltip" aria-expanded="false">
                Projected earning growth
            </button>
        </div>
    </div>
    <h2 class="mb-8 text-left">18.10%</h2>
</div>

CodePudding user response:

If I understood you correctly, you want to go from Projected earning growth to 18.10% which is in h2 tag. Not sure why you've mentioned h1 tag though.

//button[contains(text(),'Projected earning growth')]/../../following-sibling::h2

should get the job done,

Note that /.. to go to go level up in HTMLDOM.

This is straight up solution to the problem that you are having.

I would also suggest you to have a look on xpath parent and ancestor.

  • Related