Reputation: 1447
I have two links on a page that have the text 'London'. I want to choose the second one on the page, but I want to define the xpath in a way that it chooses by the parent div, but I want to use wildcard in case the link is moved.
So, the two xpaths are
//div[@id="first-id"]/div/div[2]/a[text()="London"]
//div[@id="second-id"]/div[2]/div[3]/div/a[text()="London"]
I want to use a wildcard and define the xpath within the parent div:
i.e. //div[@id="second-id"]/*/a[text="London"]
I already understand I can just use the full xpath and not have any wildcards, but I want to know if there's a way to do what I am proposing using xpath. I thought maybe contains() in some way would work but am not familiar enough with it.
Upvotes: 0
Views: 163
Reputation: 723598
To find the a
element wherever it may appear within the div
element, the descendant path is represented simply by //
:
//div[@id="second-id"]//a[text="London"]
Upvotes: 2