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:开发者_JAVA百科
//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.
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']
For Selenium you can give it as link="Logoff"
where Logoff is the text in the link.
精彩评论