使用水豚查找具有特定子元素的元素

Find element with specific child element with capybara

我正在尝试编写一个测试,我需要在其中找到一个包含特定图标

a 元素

<a href="#"><span class="icon icon-checkmark></span></a>

我试过使用 xpath,但我猜我做错了什么..

save_button = find(:xpath, '//a[span(., "icon-checkmark")]')

找到我的保存按钮的正确方法是什么?

试试这个:'*//a/span[contains(@class,'icon-checkmark')]'

你很接近,只是你需要指定你正在寻找一个 class 名字。

find(:xpath, ".//a[.//span[contains(concat(' ',@class,' '), ' icon-checkmark ')]]")

concat 和额外的空格是为了确保它匹配特定的 class 名称,而不是其他 class 名称的子字符串。