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>
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
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments