3

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

Lucero
  • 59,176
  • 9
  • 122
  • 152
Oscar C
  • 51
  • 1
  • 7

3 Answers3

2

I had the same issue, and the other solutions proposed here didn’t work for me, so used get_element_by_id.

So basically:

source = Hpricot(@selenium.get_html_source)
source.get_element_by_id("foo:bar")

Hope this helps.

edouardbriere
  • 1,170
  • 8
  • 12
2

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
  • 8,191
  • 4
  • 38
  • 39
1

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
  • 3,139
  • 2
  • 21
  • 19
  • 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 Jun 29 '10 at 15:10
  • 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 – Oscar C Jun 30 '10 at 09:57