Pankaj - PK
Pankaj - PK

Reputation: 43

Get XPath of element where parent contains specific text

I have code which looks like below and I want to click on 'View Details' but where tag p contains text as 'Dublin' How can I get xpath?

<div class="office-name">
    <p class="">Dublin</p>
    <p class="abc">
        <a target="abc" >View map</a>
        <a href="">View details</a>
    </p>
</div>

Upvotes: 0

Views: 1529

Answers (2)

Richard
Richard

Reputation: 9029

I am assuming this is how the html looks:

<div class="office-name">
    <p class="">Dublin
        <a target="abc" >View map</a>
        <a href="">View details</a>
    </p>
    <p class="abc">
        <a target="abc" >View map</a>
        <a href="">View details</a>
    </p>
</div>

Given that, one xpath that will work is:

//p[contains(text(), 'Dublin')]/a[contains(text(),'View details')]

Okay, sorry, I misunderstood your HTML. This selector will work, but I don't know if following is supported:

//div[@class='office-name']/p[contains(text(), 'Dublin')]/following::p/a[contains(text(),'View details')]

Upvotes: 0

paul trmbrth
paul trmbrth

Reputation: 20748

Try

//p[contains(text(), 'Dublin')]
 /following-sibling::p[@class="abc"][1]
     /a[contains(text(),'View details')]

Upvotes: 1

Related Questions