ratsstack
ratsstack

Reputation: 1021

Can't find unique xpath for clickable element

I'm trying to get an xpath so I can click a link as per href below:

<div id="viewIFL" style="">            
    <div class="moneycentrallink">
        Track your cash in one place with 
        <a href="/abc1/1/7979879yih9y9haidhyhw9y989y9/goto/feature/icflink?navigator=2" target="_top" xpath="2">Money Central</a>
    </div>
</div>

When I use the below in ChroPath:

//a[contains(text(),'Money Central')]

It returns 2 elements matching for xpath="1" and xpath="2".

I then tried:

//a[contains(text(),'Money Central') and @xpath='2']

and at first it resolved to just 1 element found but when I tried searching again it returned 0 elements found. Also this does not work via Selenium either (returns unable to find element).

Any ideas what's going on and how I can find the unique xpath to clickable element? Thanks

Upvotes: 0

Views: 754

Answers (2)

Don't use xpath attribute in your xpath as ChroPath adds the xpath attribute in element to tell the user what is matching occurrence of that element. For example- If ChroPath added xpath=5 i.e. this element is the 5th for the corresponding xpath. For your scenario, please inspect the element and see what ChroPath gives the relative xpath. Also you can try //div[contains(text(),'Track your cash')]//a[contains(text(),'Money Central')]

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

Your problem is badly formulated.

There is always a unique path to an element of the form *[1]/*[4]/*[1]/*[2]. The problem is that this path isn't very useful because it only works if you know exactly what is in the document, and if you knew exactly what was in the document, you wouldn't need XPath to find it.

So you're actually looking for an XPath that will work on a set of possible documents in which some parts are known (fixed) and others are unknown (variable). To find an XPath that works on every document in that set, you need to define what is known and what is unknown. Looking at one sample document isn't going to tell you that.

Upvotes: 0

Related Questions