XPath for selecting a section of an article

前端 未结 2 1199
醉话见心
醉话见心 2021-01-17 02:59

Suppose a section of an article is as follows (the html source):

Introduction

....

References

...a bunch of tex
相关标签:
2条回答
  • 2021-01-17 03:15

    if you want elements until next h2 use such xpath

    //*[following-sibling::h2[preceding-sibling::h2[1][contains(.,'References')]]  and preceding-sibling::h2[contains(.,'References')]]
    

    Wath does it mean: it finds all element which has

    -- ahead h2 which has the 1st preceding h2 containing 'References'

    -- back h2 containing 'References'

    The 1st rule takes all elements from begining of xml until next h2 tag. The 2nd -all after necessary h2 tag to end of xml. Intersection of them gives needed elements.

    Or xpath maybe build on your suggestion:

    //h2[.='References']/following-sibling::*[preceding-sibling::h2[1][contains(.,'References')] and not(name()='h2')]
    

    take all after necessary h2 tag //h2[.='References']/following-sibling::* which is not h2 and has our h2 tag as the 1st h2 before

    0 讨论(0)
  • 2021-01-17 03:32

    Xpath for above text would be

    //h2[text()='References']

    And if you want check for The correctness of Above xpath Then open webpage i chrome right click and inspect element,click ESC button to open console Of developer tool and type like This

    $x("//h2[text()='References']") and hit enter

    It will give you one html code hover on that line and see it is highlighting "References" text or not if it is highlighting the text means xpath is correct

    0 讨论(0)
提交回复
热议问题