Question

Assume I have the following HTML snippet

<div id="parent_div">

    <!-- First div -->
    <div>
        Hi <span>Dave</span>, how are you?
    </div>

    <!-- Second div -->
    <div>
        Hi, how are you?
    </div>

    <!-- Third div -->
    <div>
        Hi <span>Jenny</span>, how are you?
    </div>

    <!-- Fourth div -->
    <div>
        <span>Ryan</span>
    </div>
</div>

I would like to use XPATH to find the divs that contain BOTH a span and text. So, in the above example, I would like to find the first div and the third div only. The second div cannot be selected because it does not contain a span (contains text only), and the fourth div cannot be selected because it only contains a span (does not contain text)

How can I do that in XPATH? I am using PHP if that matters.

Was it helpful?

Solution

You can try using this XPath :

//div[text()[normalize-space(.)] and span]

Above XPath will select <div> element that has child : <span> element and non-empty text node (to skip text nodes containing only line-breaks)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top