1

I try to get a href from an anchor according to linkText given with func param (for example here Westwood or Linda). In the code below I changed classes values (cause of company info), but beside thats the code. For each <tr> the hrefs are identical for both <td>, but different from <tr> to <tr> I get the 1st href with:

driver.findElemnt(By.xpath("//a[@class = 'class Name of a']").getAttribute("href")  

or the same with:

xpath="//a[@class = 'class Name of a'][1]";

but when I go:

"//a[@class = 'class Name of a'][2]"  or 3 or 4... 

or when I use:

By.partialLinkText 

It fails, so I never get beyond the 1st <td> of the first <tr>.

The Error: oopenqa.selenium.StaleElementReferenceException : Element is no longer attached to the Dom.

The Elements are visible all the time, so its not a visibility problem.

This is the html:

<tr class="Class name for tr here">       
    <td headers="description _ CustomerList:lastName">
        <a href="some path index=0" class="class Name of a">Westwood</a>
    </td>

    <td headers="description _CustomerList:firstName">   
        <a href="some path index=0" class="class Name of a">Linda</a>
    </td>
</tr>
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
user3170595
  • 109
  • 1
  • 1
  • 12
  • Please, at least have a short glimpse at the preview to realize all your HTML tags are (now have been) missing... – Jens Erat Mar 02 '14 at 23:14
  • General hint for matching HTML class attributes: http://stackoverflow.com/questions/8808921/selecting-a-css-class-with-xpath/9133579#9133579 – Jens Erat Mar 02 '14 at 23:23

1 Answers1

1

You're trying to retrieve the second anchor tag within the same parent (in the example document: table cells).

Instead of

//a[@class = 'class Name of a'][2]

query the second anchor tag in the whole document:

(//a[@class = 'class Name of a'])[2]

Some more examples how to use positional predicates (I omitted the class predicate here):

  • Anchor tags from all second table cells per row:

    //td[2]/a
    
  • Second anchor tag per row:

    //tr//a[2]
    
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Thanks guys, it worked. I gave up the class attribute, went up to the table attribute and retrive it correctly. works! – user3170595 Mar 03 '14 at 01:23