Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the next element with xpath

Tags:

html

xml

xpath

I have the following html block, I'm trying to get all the city links under Alabama in this case. This is the xpath I have constructed

//h3/a[contains(text(),'Alabama')]/following::ul/li/a

But The xpath above matches too many things. Any idea what I am doing wrong?

<div class="geoUnit">
              
                <h3><a href="http://example.com/" style="background-color: rgba(0, 0, 0, 0);">Alabama</a></h3>
                  <ul style="background-color: rgba(0, 0, 0, 0);">
                    <li style="background-color: rgba(0, 0, 0, 0);"><a href="http://example.com/" style="background-color: rgba(0, 0, 0, 0);">Auburn</a></li>
                    <li><a href="http://example.com/">Birmingham</a></li>
                    <li><a href="http://example.com/">Dothan</a></li>
                    <li><a href="http://example.com/">Gadsden</a></li>
                    <li><a href="http://example.com/">Huntsville</a></li>
                    <li><a href="http://example.com/">Mobile</a></li>
                    <li><a href="http://example.com/">Montgomery</a></li>
                    <li><a href="http://example.com/">Muscle Shoals</a></li>
                    <li><a href="http://example.com/">Tuscaloosa</a></li>
                 </ul>
</div>

I want the following to be selected as an array in this case. I want to extract each city link. I am using xpath inside of Java

                    <li style="background-color: rgba(0, 0, 0, 0);"><a href="http://example.com/" style="background-color: rgba(0, 0, 0, 

0);">Auburn</a></li>
                        <li><a href="http://example.com/">Birmingham</a></li>
                        <li><a href="http://example.com/">Dothan</a></li>
                        <li><a href="http://example.com/">Gadsden</a></li>
                        <li><a href="http://example.com/">Huntsville</a></li>
                        <li><a href="http://example.com/">Mobile</a></li>
                        <li><a href="http://example.com/">Montgomery</a></li>
                        <li><a href="http://example.com/">Muscle Shoals</a></li>
                        <li><a href="http://example.com/">Tuscaloosa</a></li>
like image 688
Arya Avatar asked Jun 29 '26 01:06

Arya


1 Answers

I'm trying to get all the city links under Alabama in this case.

Your XPath does select all a that follow the targeted heading.

But The xpath above matches too many things.

Perhaps there are additional ul elements following the targeted heading in your complete document. In this case, you can append a [1] to the ul step in your XPath to limit the selection to the immediately following ul element:

//h3/a[contains(text(),'Alabama')]/following::ul[1]/li/a

or, cleaned-up a bit:

//h3[a='Alabama']/following-sibling::ul[1]/li/a

This will limit the a elements to those of just the immediately following ul.

like image 58
kjhughes Avatar answered Jun 30 '26 15:06

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!