我正在使用 capybara 运行一些 cucumber 功能,我需要检查是否正在显示某个图像。
我尝试了这个 xpath 匹配,但显然是函数 matches
不可用:
//img[matches(@src, "my_image.png")]
最佳答案
您不需要任何 matches
功能。用:
//img[@src='my_image.png']
或者,如果路径可以在要匹配的部分之前包含文本:
//img['my_image.png'=substring(@src, string-length(@src) - 11)]
第二个表达式模仿了
ends-with
功能。如果您不喜欢对子字符串长度进行硬编码,请使用:
//img['my_image.png'=substring(@src,
string-length(@src) - string-length('my_image.png') + 1)]
为了完整性:在某些情况下,以下是可以接受的:
//img[contains(@src, 'my_image.png')]
关于xpath - 如何使用xpath按文件名查找图像标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5738972/