-2

I have been having some difficulties finding an XPath for the following H

<div>
    <p> pppppppp 
        <span class="rollover-people">
            <a class="rollover-people-link">pppppp</a>
            <span class="rollover-people-block">
                <span class="rollover-block">
                    <span>
                        <img src="/someAddress" width="100" height="100" alt>
                        <a>xxxx</a>
                        <a>xxxxx</a>
                    </span>
                </span>
            </span>
        </span>pppppppp
    </p>ppppppppp
<div>

So basically I need everything inside the <p> up to <span class="rollover-people-block">. In another word, I want <p> but not <span class="rollover-people-block">. Is that even possible? Keep in mind, the <p> gets repeated more than once in the page.

CodeMonkey
  • 2,511
  • 4
  • 27
  • 38
  • No. It's not possible (with XPath only) to get element **excluding** particular descendants – JaSON Sep 04 '19 at 18:57

1 Answers1

1

This is what something closure you are looking for.

//p//text()[not(ancestor::span[@class='rollover-people-block'])]

This will get all the text nodes under p excluding the ones which are under span class='rollover-people-block'.

Sample html:

<!DOCTYPE html>
<html>
 <body>
 <div>
    <p> A 
        <span class="rollover-people">
            <a class="rollover-people-link">B</a>
            <span class="rollover-people-block">
                <span class="rollover-block">
                    <span>
                        <img src="/someAddress" width="100" height="100" alt>
                        <a>c</a>
                        <a>d</a>
                    </span>
                </span>
            </span>
        </span>E
    </p>f
 <p> G
        <span class="rollover-people">
            <a class="rollover-people-link">H</a>
            <span class="rollover-people-block">
                <span class="rollover-block">
                    <span>
                        <img src="/someAddress" width="100" height="100" alt>
                        <a>i</a>
                        <a>j</a>
                    </span>
                </span>
            </span>
        </span>K
    </p>l
<div>
 </body>
</html>

xpath output:

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • this actually works! although the spacing is a little off but it actually grabs the text which does the job! thanks for the help mate. – CodeMonkey Sep 05 '19 at 14:28