1

For example:

I have the following structure:

<td>  
  <a>  
  <input id="MyID1">  
</td>

<td>  
  <a>  
  <input id="MyID2">  
</td>  

<td>  
</td>  

We suggest that <a> does not have any specific attributes to locate it by them.

So 2 questions:

  1. I need to locate <a> in second td. I know that <a> Im looking for is placed to the same td with "MyID2" input. How can I do that?

  2. I need to locate 3rd td (empty). I know that td Im looking for is the following td for td what contain "MyID2" input. How can I do that?

Thanks!

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
m-80
  • 11
  • 1

1 Answers1

0

If you can use XPath somewhere, the following expression will find the <a> element in the second <td>:

//td/a[../input/@id='MyID2']

It will find all <a> elements, which are direct children of a <td> element having an <input> element on the same level with an id attribute equal to 'MyID2'.

jarnbjo
  • 33,923
  • 7
  • 70
  • 94
  • It's been a while, but I don't believe XPath works on non-well-formed structured text. Here's a reference to a similar question: http://stackoverflow.com/questions/354322/finding-a-node-or-close-to-it-using-xpath-in-non-well-formed-html – Riggy Jan 18 '11 at 12:56
  • 1
    All browsers transform non-well-formed HTML code into a "browseable", well-formed DOM model. E.g. JavaScript XPath libraries use this model to allow XPath queries for searching and accessing HTML elements. I am not too familiar with Selenium, but I would assume that Selenium works in a similar fashion. Hence my "if you can use XPath" introduction. – jarnbjo Jan 18 '11 at 13:49