Q:

How can I find an element with XPath using its parent?

I need to get the "a" element inside a "td" element from a row in a table of several similar rows. The problem is I only have the name 'john'. How can I find john td -> get the parent "tr" -> and then get "a" in XPath?
Code example:
<?xml version="1.0" encoding="UTF-8"?>
<html>
  <table>
    ...
    <tr id='1'>
      <td name='john'>
      </td>
      <td>
        <a id='clickable'/>
      </td>
      <td>
      </td>
    </tr>
    ...
  </table>
</html>

A:

I would write this XPath expression like this:
//td[@name="john"]/following-sibling::td[1]/a

This does:

//

from any depth

td

find a td element

[@name="john"]

with a name attribute equal to 'john'

/following-sibling::

now look among its following sibling elements

td

and find another td

[1]

get the first one

/a

and get its children that are a elements

A:

How about:
//a[ancestor::tr[td/@name = 'john']]

A:

What I would do :
//*[@name="john"]/../td/a/@id

