微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 web 表中的 xpath 使用其他两列值获取中间列值

如何解决使用 web 表中的 xpath 使用其他两列值获取中间列值

<tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>

[https://www.w3schools.com/html/html_tables.asp]

您可以在链接中找到示例 Web 表格。我想要与公司和国家的联系价值。

我试过这样的事情

//td[text()='Laughing Bacchus Winecellars']//following-sibling::td and //td[text()='Canada']//preceding-sibling::td

解决方法

您可以循环遍历 <tr> 并使用 //tr/td[2] 选择第二个 <td>

http://xpather.com/aHyybZni

,

试试这个选择所需的 td 节点:

//td[preceding-sibling::td[.="Alfreds Futterkiste"] and following-sibling::td[.="Germany"]]
,

尝试以下 xpath:

//td[preceding-sibling::td[text()='Laughing Bacchus Winecellars'] and following-sibling::td[text()='Canada']]
,

要提取中间列值,即 Maria Anders,您需要为 visibilityOfElementLocated() 引入 WebDriverWait,您可以使用以下任一 {{3} }:

  • 使用公司文本Alfreds Futterkiste

    • xpath

      //table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]
      
    • 代码行:

      System.out.println(new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]"))).getText());
      
  • 使用国家文本德国

    • xpath

      //table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]
      
    • 代码行:

      System.out.println(new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]"))).getText());
      

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。