简体   繁体   中英

How to select content of H3 if H2 contains certain text

I would like to scrape the content of the H3 element if the H2 just above it contains a certain text, in this case the H2 should contain "Course advanced Sydney".

But I can't get the Xpath to work. Someone who can solve this issue? Unfortunately I'm not very experienced with Xpath. But I understand the basics.

I tried this with Xpath:

//div[@id='training']/article/[h2/text() = 'Course advanced Sydney']/h3

The code:

    <div id="training" class="overview">
        <article>
            <h2>Course beginner Amsterdam</h2>
            <h3>20 May 2019</h3>
        </article>
        <article>
            <h2>Course advanced Sydney</h2>
            <h3>27 May 2019</h3>
        </article>
        <article>
            <h2>Course beginner Sydney</h2>
            <h3>6 June 2019</h3>
        </article>
        <article>
            <h2>Course medior New York</h2>
            <h3>16 June 2019</h3>
        </article>
    </div>

I expect the output to be "27 May 2019", but the actual output is "#N/A"

The h3 element is not a descendant of the h2 , it is its sibling. So use

//div[@id='training']/article/h2[contains(., 'Course advanced Sydney']/following-sibling::h3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM