找到按钮的所有 XPath 并选择最后一个?
Find all XPaths of button and chose the last one?
我有一个 table 有四列,行数越来越多。每次我测试时,我都需要在最后一行应用测试,我不知道它的 XPath,因为我第一次编写测试时它不在那里。
我需要的 XPath 如下:
//*[@id="example"]/div/table/tbody/tr[X]/td[4]/a[1]
带有变量 X
。
有没有办法废弃 X
值并选择它的最大值,或者类似的东西?
您可以使用:
//*[@id="example"]/div/table/tbody/tr[last()]/td[4]/a[1]
这将根据需要从最后 table 行的第四个 td
元素开始 select 第一个锚标记。
最简单的解决方案可能是
all(:xpath, '//*[@id="example"]/div/table/tbody/tr/td[4]/a[1]', minimum: 1).last
或
all('#example div table tr td:nth-of-type(4) a:first-of-type', minimum: 1).last
注意:minimum: 1
强制 #all
使用 Capybaras 等待行为来等待至少 1 个匹配元素,就像 #find
通常那样
我有一个 table 有四列,行数越来越多。每次我测试时,我都需要在最后一行应用测试,我不知道它的 XPath,因为我第一次编写测试时它不在那里。
我需要的 XPath 如下:
//*[@id="example"]/div/table/tbody/tr[X]/td[4]/a[1]
带有变量 X
。
有没有办法废弃 X
值并选择它的最大值,或者类似的东西?
您可以使用:
//*[@id="example"]/div/table/tbody/tr[last()]/td[4]/a[1]
这将根据需要从最后 table 行的第四个 td
元素开始 select 第一个锚标记。
最简单的解决方案可能是
all(:xpath, '//*[@id="example"]/div/table/tbody/tr/td[4]/a[1]', minimum: 1).last
或
all('#example div table tr td:nth-of-type(4) a:first-of-type', minimum: 1).last
注意:minimum: 1
强制 #all
使用 Capybaras 等待行为来等待至少 1 个匹配元素,就像 #find
通常那样