2
votes

I want to use XPath to locate a div (class="class1") element which encapsulates both strings 'Text1' and 'Text2' in this html:

<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>

But this xpath does not seem to work:

//div[@class="class1" and contains(text(),"Text1") and contains(text(),"Text2")]

How can I do this?

1

1 Answers

6
votes

Don't use text(), as it's related only to the immediate text children of the node:

//div[@class="class1" and contains(., "Text1") and contains(., "Text2")]