小编g0h*_*g0h的帖子

XPath:定位包含两个字符串的文本节点的元素

我想使用 XPath 来定位一个 div (class="class1") 元素,它在这个 html 中封装了字符串 'Text1' 和 'Text2':

<html>
<head>
    <title></title>
</head>
<body>
    <div class="class1">
        <div>
            <a href="http://url1.com">Text1</a>
        </div>
        <div>
            <div>
                <a href="http://url2.com">Text2</a>
            </div>
        </div>
    </div>
    <div class="class1">
        <div>
            <a href="http://url3.com">Text3</a>
        </div>
        <div>
            <div>
                <a href="http://url4.com">Text4</a>
            </div>
        </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

但是这个 xpath 似乎不起作用:

//div[@class="class1" and contains(text(),"Text1") and contains(text(),"Text2")]
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

html xpath

2
推荐指数
1
解决办法
1305
查看次数

标签 统计

html ×1

xpath ×1