Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath interpretation by Selenium/WebDriver driving IE

I'm trying to use Selenium RC and WebDriver (separately) to manipulate an HTML page. The source contains something like:

<a href="/logoff"><span>
    <span>L</span>
    ogoff
    </span>
</a>

I want to target the link that contains the text 'Logoff', in such a way that it will still work even if they rework the spans. I'm using XPath since that should work in both WebDriver and Selenium RC. This works in IE but is slightly fragile:

//a[contains(., 'ogoff')]

and this would be more robust but doesn't work:

//a[.='Logoff']

Can you explain why the second doesn't work? In particular, how should . be interpreted as a string, and how do Selenium and WebDriver do it?

I'm trying to get Selenium to work with Firefox as well, so that will open up another kettle of worms.

like image 423
Bennett McElwee Avatar asked May 04 '11 06:05

Bennett McElwee


2 Answers

There is whitespace (spaces and carriage returns) that are seen by the processor as significant and affecting the computed value of a.

When you select the value of a like this *<xsl:value-of select="a"/>* the result is:

* L
    ogoff
     *

If you use *<xsl:value-of select="normalize-space(a)"/>* the result is:

*L ogoff*

If you want to be able to select for "Logoff", you could use normalize-space() and then wrap it with translate() to remove the spaces:

//a[translate(normalize-space(), ' ','')='Logoff']
like image 124
Mads Hansen Avatar answered Oct 29 '22 03:10

Mads Hansen


For Selenium you can give it as link="Logoff" where Logoff is the text in the link.

like image 28
A.J Avatar answered Oct 29 '22 02:10

A.J