Rspec:如何期望表中的href链接

      expect(page). to have_xpath("//table//tbody//tr[1]//td[4][contains(href(),edit_data_source_group_path(data_source_group_1.id))]")

我需要期望在表内部的href“ edit_data_source_group_path(data_source_group_1.id)”,并且尝试了上面的代码。

我确定我没有在contains下传递正确的表达 并走到错误之下  Selenium :: WebDriver :: Error :: InvalidSelectorError:        给定xpath表达式“ // table // tbody // tr 1 // td [4] [包含(href(),edit_data_source_group_path(data_source_group_1.id))]”无效:SyntaxError:该表达式不是法律表达。

我还附上了我需要解决的表格快照 有人可以帮助我如何期望表格

Rspec:如何期望表中的href链接

中包含适当的href
rrrttt1984 回答:Rspec:如何期望表中的href链接

首先,您应该停止尝试在一个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

大家都在问