首页 > 解决方案 > Watir 返回不正确的子表

问题描述

我有两个具有相同属性 ( random-attribute) 的表,这是识别它们的最独特方式。区分它们的唯一方法是通过它们所属的父 div(参见下面的示例)。

<div class="header">
    <table random-attribute="left-desc">
    </table>
    <table random-attribute="main-desc">
    </table>
<div>
<div class="body">
    <table random-attribute="left-desc">
    </table>
    <table random-attribute="main-desc">
    </table>
<div>

我正在尝试获取main-desc属于 的表的内容,<div class="body">但下面的代码不断返回我的表内容<div class="header">

browser = Watir::Browser.new
browser.goto(MY_URL_PAGE)
browser.div(class: "body").table(xpath: '//table[@random-attribute="main-desc"]')

奇怪的部分是,如果我运行browser.div(class: "body")它会返回正确表的内容,但是当我链接它时,browser.div(class: "body").table(xpath: '//table[@random-attribute="main-desc"]')它会返回表browser.div(class: "header")

标签: ruby-on-railsrubywatir

解决方案


记住包含 XPath 的初始点是 Watir 试图避免使用 XPath 的原因之一。要避免 XPath,您可以执行以下操作:

browser.div(class: "body").table(random_attribute: "main-desc")

推荐阅读