如何找到与Xpath匹配某个字符串的元素?

时间:2020-12-30 19:23:40

I would know if it's possible to find all elements that match a certain string, using . For example, suppose I have this page:

我想知道是否可以使用xpath找到与某个字符串匹配的所有元素。例如,假设我有这个页面:

<html>
<head>
    <title></title>
</head>
<body>
    <form id="form1">
    </form>
    <p class="test"></p>
    <p class="test"></p>
    <p class="test"></p>
    <p class="test"></p>
</body>
</html>

If I search for this string<form id="form1"> I would get the first form element, instead if I search for this string <p class="test"></p> I would get all the paragraphs elements. Is it possible? Something like //*[matches(., string)] I'm at the beginning, so any suggestions will be appreciated.

如果我搜索这个字符串

我会得到第一个表单元素,相反,如果我搜索这个字符串

我会得到所有段落元素。可能吗?像// * [匹配(。,字符串)]之类的东西我刚开始,所以任何建议都会受到赞赏。

1 个解决方案

#1


1  

Try this using :

//form[@id="form1"]

Output :

<form id="form1">
</form>

The rest :

//p[@class="test"]

and if you want a partial match :

如果你想要部分匹配:

//p[contains(@class, "tes")]

#1


1  

Try this using :

//form[@id="form1"]

Output :

<form id="form1">
</form>

The rest :

//p[@class="test"]

and if you want a partial match :

如果你想要部分匹配:

//p[contains(@class, "tes")]