views:

59

answers:

2

Hi,

I'm using Hpricot with selenium I have this html input element:

<input id="foo:bar"/>

And I'm trying to get this value with this Xpath expression:

source = Hpricot(@selenium.get_html_source)
source.search("//input[@id='foo:bar']")

but it is not finding anything because of the colon. I have seen that the Xpath expression cannot contain any colon. I have tried to escape it in different ways but it doesn't work.

Is there any way to escape it or avoid this problem? I cannot change the values in the html so the foo:bar has to be this way, with the colon. But I need to find this element somehow.

Any ideas?

thanks

+1  A: 

You may be able to achieve this with a css locator, and specify that the id contains a particular string (the fact this is matching a string should remove any weirdness caused by the colon). This locator worked for me when I tried it in Selenium IDE:

css=input[id:contains('foo:bar')]
AlistairH
I'm not familiar with hpricot, so couldn't tell you how to convert into the appropriate syntax for that, but it does appear to support CSS locators.
AlistairH
I was seeing in the HPricot documentation that you can use either a XPath expression or css locator. I was trying your suggestion but it didn't work, not sure if it was my mistake though, I'm new with xpath and selenium.. I found a workaround so I can get get the unique element with the second part of the id + other attribute. thanks
+2  A: 

You don't need to use XPath for this locator, as long as the id is unique you can simply use:

id=foo:bar

Even then, the id= prefix is not necessary as it will be assumed by default.

Dave Hunt