How to select an element that depends on another by XPath

1

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

I'm currently filtering Meu elemento using the following snippet:

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

...
<div>
  <table>
    <tbody>
      <tr>
        <td>
          <a>Dependencia</a>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div>
  <div>Meu elemento</div>
</div>
    
asked by anonymous 13.11.2017 / 14:52

2 answers

1

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

//*[contains(text(), 'Meu elemento')][//*[contains(text(), 'Dependencia')]]
    
05.12.2017 / 18:17
1

According to the XML structure you included above, the XPath below selects the contents of div within another div that is immediately preceded by a div containing a table that has a containing cell 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 does not work you should include more details of your HTML / XML (omitted tags, namespaces, etc.)

    
04.12.2017 / 22:24