Ilja
Ilja

Reputation: 1215

XPath to extract element if one of its children has some text

Is it possible to craft an XPath query to extract some (html) element if its children has some text, in case, if nesting level is not known ?

For example, html to extract from:

    <a href="/dialog/id/7">
        <div class="someclass_1">... random text</div>
        <div class="someclass_2">Text_need_to_find</div>
            <div class="subclass_1">
                Text_need_to_find
           <div class="subclass_2">... random text</div>
                <div class="subclass_3>
                    Text_need_to_find
                </div>
            </div>
    </a>        

And XPath is required to get href attribute if any element or subelement has text Text_need_to_find.

So basically I want to get /dialog/id/7 if in any of elements children has text Text_need_to_find

Upvotes: 0

Views: 162

Answers (1)

hr_117
hr_117

Reputation: 9627

Should be something like:

a[descendant-or-self::*[normalize-space() = 'Text_need_to_find']]/@href

if the "Text_need_to_find" is only a part of the content use contains(., 'Text_need_to_find').

Upvotes: 4

Related Questions