Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an image tag by filename using xpath

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")]
like image 847
Macario Avatar asked Apr 21 '11 02:04

Macario


People also ask

How to get XPath of img tag in selenium?

WebElement temp = driver. findElement(By. xpath("//img[contains(@src,'web/L001/images/IMAGENAME. jpg')]"));


1 Answers

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')]
like image 167
Wayne Avatar answered Sep 21 '22 03:09

Wayne