首先,您应该停止尝试在一个XPath中完成所有操作。它使事情变得非常脆弱,难以阅读,而且很痛苦。假设页面上只有一个表格,您可以执行以下操作
cell = page.find(:xpath,'.//tbody/tr[1]/td[4]')
expect(cell).to have_link(href: edit_data_source_group_path(data_source_group_1.id))
或使用CSS代替XPath
cell = page.find('tbody > tr:first-child > td:nth-child(4)')
expect(cell).to have_link(href: edit_data_source_group_path(data_source_group_1.id))
如果页面上有多个表,则可以将范围限定在正确的表上,或者可以使用过滤器块来完成此操作
expect(page).to have_xpath('.//tbody/tr[1]/td[4]') do |cell|
cell.has_link?(href: edit_data_source_group_path(data_source_group_1.id))
end
如果页面上只有一个指向该特定位置的链接,您还可以使用
之类的方法来逆转该过程。
link = page.find_link(href: edit_data_source_group_path(data_source_group_1.id))
expect(link).to match_css('td:nth-child(4) a')
请注意-所有以.//
开头的XPath-这很重要-https://github.com/teamcapybara/capybara#beware-the-xpath--trap-除非绝对必要,否则还有一个不使用XPath的原因。
,
您在xpath中出现错误,不应使用href(),而只能使用href,请尝试类似的操作:
期望(页面)。到have_xpath(“ // table // tbody // tr [1] // td [4] [包含(href,edit_data_source_group_path(data_source_group_1.id))]”)
本文链接:https://www.f2er.com/3117922.html