tags:

views:

256

answers:

1

After switching from firefox testing to internet explorer testing, some elements couldn't be found by selenium anymore.

i tracked down one locator:

xpath=(//a[@class='someclass'])[2]

While it works as it should under firefox, it could not find this element in ie. What alternatives do i have now? JS DOM? CSS Selector? How would this locator look like?

Update:

I will provide an example to make my point:

<ul>
  <li>
    <a class='someClass' href="http://www.google.com"&gt;BARF&lt;/a&gt;
  </li>
  <li>
    <a class='someClass' href="http://www.google.de"&gt;BARF2&lt;/a&gt;
  </li>
</ul>
<div>
  <a class='someClass' href="http://www.google.ch"&gt;BARF3&lt;/a&gt;
</div>

The following xpath won't work:

//a[@class='someclass'][2]

In my understanding this should be the same as:

//a[@class='someclass' and position()=2]

and i don't have any links that are the second child of any node. All i want is, to address one link from the set of links of class 'someClass'.

+2  A: 

Without knowing the rest of your HTML source it's difficult to give you alternatives that are guaranteed to work. Hopefully the following suggestions will help point you in the right direction:

  • //a[@class='someClass'][2]
    This is like your example, but the parantheses are not needed.

  • //a[contains(@class, 'someClass')][2]
    This will work even if the link has other classes.

  • css=a.someClass:nth-child(2)
    This will only work if the link is the 2nd child element of it's parent.

Update

  • Based on your update, try the following:
    //body/descendant::a[@class='someClass'][2]
Dave Hunt
i agree with your recommended solution, but you just missed a @, since class is an attribute
Rubens Farias
True, i didn't provide the context, but let me explain my intention with this xpath:Those parantheses define a set of all links with specified class on the page. And from this set i want to access the 2nd element. Those links are not siblings, they can be potentially scattered across the page. I don't know any other location method with selenium than xpath that could achieve this sort of locating. And the question remains, why does this not work on IE, but in FF.thx
ruby fu novice
Have you tried the recommended solution in IE? It should work just fine.
Dave Hunt
@Rubens Thanks! Pretty critical typo there! :)
Dave Hunt
thx for the effort, but it didn't help for my case. i added an example code snippet in the description above to make it more clear.
ruby fu novice
Updated answer with new suggested solution.
Dave Hunt
Excellent, that did the trick. Once you see it, it's kind of obvious, but until then x).
ruby fu novice