Reputation: 430
I have the following HTML structure. Each <div>
represents a state and each <a>
tag represents a city within that state
<div class="country">AL</div>
<a href=somelink>City1</a>
<a href=somelink>City2</a>
<a href=somelink>City3</a>
<a href=somelink>City4</a>
<a href=somelink>City5</a>
<div class="country">CA</div>
<a href=somelink>City21</a>
<a href=somelink>City22</a>
<a href=somelink>City23</a>
<a href=somelink>City24</a>
<div class="country">IL</div>
<a href=somelink>City31</a>
<a href=somelink>City32</a>
<a href=somelink>City33</a>
<a href=somelink>City34</a>
I need to extract all a tags that belong to a certain state.
I tried this:
//*[contains(text(), "CA")]/following-sibling::a[preceding::div]
But it got me
City21 City22 City23 City24 City31 City32 City33 City34
while I only wanted
City21 City22 City23 City24
Upvotes: 1
Views: 348
Reputation: 52685
Try below XPAth
expression:
//a[count(following-sibling::div)=count(//div[text()="AL"]/following-sibling::div)-1]
or
//a[preceding-sibling::div[2][text()="AL"]]
Upvotes: 4
Reputation: 26153
You can select tags a
such that the first div
before contains the state abbreviation:
//a[preceding::div[1][contains(text(), "CA")]]
Upvotes: 1