Home getting the next element with xpath
Reply: 1

getting the next element with xpath

Arya
1#
Arya Published in 2017-12-08 00:38:43Z

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>
kjhughes
2#
kjhughes Reply to 2017-12-08 00:47:06Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.372466 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO