0

Let's say I have a section of an xpath that looks like

<parent elements>
    <div>
        <h2>Dog</h2>
    <div>
        <h2>Cat</h2>
        <table>
            <tbody>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Some Text</tr>
                <tr>Target</tr>
            </tbody>
    <div>

I first need to narrow it down to only the div where the value inside the h2 tag is == "Cat". After this, I need to drill down into said div and extract the 5th tr element.

The problem I'm trying to overcome is that all the divs have unique h2's, but they are in random order so sometime the div with an h2 == "Cat" might be ./div[1] whereas other times it might be ./div[5]. Once I've identified the div with the proper h2 (unique identifier) I can then reliably always extract exactly the 5th tr element.

I'm currently trying the code below but it only returns True/False

./parent_elements[div/h2 = "Web Site Information"]

Many thanks!

1 Answer 1

1

As your example input XML is not valid, I've just adjusted it to this:

<parent_elements>
  <div>
    <h2>Dog</h2>
    <div>
       <h2>Cat</h2>
       <table>
          <tbody>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Some Text</tr>
             <tr>Target</tr>
          </tbody>
       </table>
    </div>
  </div>
</parent_elements>

For this example, the following XPath

//parent_elements//div[h2='Cat']//tr[5]

has the result

<tr>Target</tr>

This XPath selects the fifth tr that is a child of an h2 element with the value Cat which is a child element of parent_elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.