如何通过文件名使用xpath找到图像标记 [英] How to find an image tag by filename using xpath
本文介绍了如何通过文件名使用xpath找到图像标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用capybara运行一些黄瓜功能,我需要检查是否显示某个图像。
I am running some cucumber features using capybara and I need to check if a certain image is being shown.
我尝试这个xpath匹配,但显然是函数匹配
不可用:
I tried this xpath match but apparently the function matches
is not available:
//img[matches(@src, "my_image.png")]
推荐答案
任何匹配
函数。使用:
//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')]
这篇关于如何通过文件名使用xpath找到图像标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文