How to select an element that depends on another by Xpath

Asked

Viewed 508 times

1

I need to build a xpath that returns the Meu elemento only if the Dependenciais present on the screen.

I am currently making the filter of Meu elemento using the section below:

//div[contains(text(), 'Meu elemento')]

...
<div>
  <table>
    <tbody>
      <tr>
        <td>
          <a>Dependencia</a>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div>
  <div>Meu elemento</div>
</div>

2 answers

1


I was able to solve my problem with the following xpath:

//*[contains(text(), 'Meu elemento')][//*[contains(text(), 'Dependencia')]]

1

According to the XML structure you included above, the Xpath below selects the content of div within another div which is immediately preceded by a div containing a table with a cell containing a a with the exact content 'Dependency':

//div/div[preceding::div/table//a[text() = 'Dependencia']]

This works for the substructure you included above. If it doesn’t work you should include more details of your HTML/XML (omitted tags, namespaces, etc.)

  • 1

    As you mentioned, any structure that was changed in XML would result in losing the final result. It seemed to me a good solution, but hard coded.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.