getting the next element with xpath

Arya

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

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.

edited at
0

Comments

0 comments
Login to comment

Related

Getting next sibling element using XPath and Selenium for Java

Getting next element text

Getting the next element in a pandas dataframe

Getting next element in a table javascript

Dictionary - Getting the next element of a known element

XPath for next/sibling/following HTML element?

XPath get next element using contains text()?

xpath selenium locating next sibling - any element

Getting an xml element value using xpath

JDOM XPath Getting Inner Element without Namespace

Getting nextSibling of element using xpath and puppeteer

Trouble getting text after an element using xPath

Getting next element while cycling through a list

Getting focus on next input element anywhere on the page

Using Xpath find element by text and print the next element text in python

XPath getting text from an element after a certain element

Getting sub element value using XPath where date is the element value

Find an Element in xPath where the next Sibling has a specific Text

XPath: Get next element where parent contains text

XPath: Getting an element where one of its childs has certain conditions

XPath- Getting Element from a table having dynamic ID

Getting the element on page source but unable to locate using xpath

Getting error on invalid xpath, but able to hit the element while inspecting on browser

Getting the index of the next element in a very large memmap which satisfies a condition

Getting text outside element but next to Elements using jsoup

Getting the next element in a list without using the index function in python

Use XPATH to locate a specific element and then move forward from that element to locate the next table in the tree

How to locate an element with div format in Selenium. getting error Unable to locate an element with the xpath expression

Getting the following Message: no such element: Unable to locate element:{“method”:”xpath”,”selector”:”//span[@id=’lblError’]/text()”}”