如何在 webdriver 中使用 Xpath 通过精确文本匹配搜索节点 [英] How to search node by exact text match using Xpath in webdriver

查看:58
本文介绍了如何在 webdriver 中使用 Xpath 通过精确文本匹配搜索节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在 webDriver 中使用 xpath 搜索精确文本,我需要一些帮助.

I need a little help regarding searching an exact text using xpath in webDriver.

假设我有如下 html..

Suppose i have the html as follows..

<html><body>
  <table>
    <tr>
      <td><button>abcd</button></td>
      <td><button>abc</button></td>
    </tr>
  </table>
</body></html>

现在我想点击按钮abc"

Now i want to click button "abc"

我使用 xpath 作为 //button[contains(text(),'abc')] 但它总是在按钮abcd"上执行,因为它也包含文本abc".在这方面,我需要一个谓词或其他一些可以搜索精确文本而不是包含文本的过程.

I used xpath as //button[contains(text(),'abc')] but it is always performing on button "abcd" as it also contain the text "abc". In this regards I need a predicate or some other procedure which can search exact text instead of containing text.

我也试过 //button[matches(text(),'abc')], //button[matches($string,'abc')]//button[Text='abc')]//button[.='abc')] 等等,但这些都无法识别ABC"按钮.

I also tried with //button[matches(text(),'abc')], //button[matches($string,'abc')], //button[Text='abc')], //button[.='abc')] and many more but none of these worked out to identify "abc" button.

我不知道我的 xpath 版本是否有任何问题,因为我不知道版本.但我使用的是 java 1.6 JDK.

I do not know if there is any problem regarding my xpath version as I'm not aware of the version. But I'm using java 1.6 JDK.

虽然我的具体场景不是显示的示例,但需要应用类似的逻辑.

Though my exact scenario is not the example shown but similar logic needs to be applied.

因此,我们将不胜感激任何帮助或建议.

Hence any help or suggestion would be highly appreciated.

推荐答案

我会使用下一个 xpath //button[text()='abc'].您提到了 text() 函数,但我不确定语法是否正确.您还尝试使用 contains() - 它搜索部分文本,WebDriver 获取找到的第一个元素.我你的情况是 button

I would use next xpath //button[text()='abc']. You have mentioned text() function but I'm not sure syntax was correct. Also you tried to use contains() -- it searches partial text and WebDriver gets first element found. I your case it is <button>abcd</button> button

这篇关于如何在 webdriver 中使用 Xpath 通过精确文本匹配搜索节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆