16

I am running some cucumber features using capybara and I need to check if a certain image is being shown.

I tried this xpath match but apparently the function matches is not available:

//img[matches(@src, "my_image.png")]
Wayne
  • 59,728
  • 15
  • 131
  • 126
Macario
  • 2,214
  • 2
  • 22
  • 40
  • If it is shown on the page you are getting it or if you got it correctly and you need to check it ? – EnexoOnoma Apr 21 '11 at 02:58
  • I would not call this an exact duplicate, but the answer is very familiar. See also: http://stackoverflow.com/questions/402211/how-to-use-xpath-function-in-a-xpathexpression-instance-programatically – Wayne Apr 21 '11 at 03:12

1 Answers1

32

You don't need any matches function. Use:

//img[@src='my_image.png']

Or, if the path can include text before the portion you want to match:

//img['my_image.png'=substring(@src, string-length(@src) - 11)]

This second expression imitates an ends-with function.

If you don't like hard-coding the substring length, then use:

//img['my_image.png'=substring(@src, 
          string-length(@src) - string-length('my_image.png') + 1)]

For completeness: in some cases, the following is acceptable:

//img[contains(@src, 'my_image.png')]
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • Thanks, I've used contains since it wasn't an exact match. – Macario Apr 21 '11 at 03:09
  • Thanks. Been looking all over for a list of xpath functions capybara supports. I was banging my head trying to figure out why my ends-with matcher doesn't work. Do you have a list of supported functions? – Derek Prior Aug 29 '11 at 21:15