如何使用 xpath 按文件名查找图像标签 [英] How to find an image tag by filename using xpath

查看:15
本文介绍了如何使用 xpath 按文件名查找图像标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 capybara 运行一些黄瓜功能,我需要检查是否正在显示某个图像.

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

我尝试了这个 xpath 匹配,但显然函数 matches 不可用:

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

//img[matches(@src, "my_image.png")]

推荐答案

你不需要任何 matches 函数.使用:

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)]

第二个表达式模仿 ends-with 函数.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆