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

g0h*_*g0h 2 html 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)

我怎样才能做到这一点?

cho*_*oba 6

不要使用text(),因为它只与节点的直接文本子节点相关:

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