How to create XPath for the Link which precede the conditional span class

466 views
Skip to first unread message

unni

unread,
Oct 14, 2013, 8:42:10 AM10/14/13
to webd...@googlegroups.com
I want to click the link (anchor tag) from the below grow-able list for YES class. How do create XPath for the same. 

I want to click the link from //li/div/h2/a/span[contains(text(), 'YES')] as I could see 'YES'

I tried self and preceding, //li/div/h2/self::a/span[contains(text(), 'YES')] but does not worked. 

<li>
<div>
<h2>
<a href="">Dynamic Link X</a>
</h2>
<span class="dynamic">YES</span>
</div>
</li>
<li>
<div>
<h2>
<a href="">Dynamic Link Z</a>
</h2>
<span class="dynamic">NO</span>
</div>
</li>

Sean Adkinson

unread,
Oct 14, 2013, 11:44:15 AM10/14/13
to webd...@googlegroups.com
This question isn't about webdriver, but I'll see if I can help.

You <span> is not in your <a>, so  //li/div/h2/a/span[contains(text(), 'YES')] won't match anything.  Assuming the HTML you posted is correct, I would try something like this:

/li/div[./span[contains(text(), 'YES')]]/h2/a

OR

/li/div/span[contains(text(), 'YES')]/../h2/a

The first solution filters on <div>s that have a "YES" span.  The second finds the "YES" span, and travels up to the parent and down to the <a>.

-Sean

unni

unread,
Oct 15, 2013, 2:20:27 AM10/15/13
to webd...@googlegroups.com
Thanks Sean for the reply. 

Both the approaches are not working. with 'YES' its clicking first link but to debug I changed to 'NO' with expectation of second link to get open.Still it clicked the first link.

Think of the UL has many link (I just portrayed 2 LI), The Link contains YES may appear anywhere under UL.So the listing is dynamic. I wanted to click first link that has 'YES' in it. likewise for 'NO'.

I think we should use preceding, forwarding,dependent, ancestor, self  some sort of this to find the correct span and click specific link.

Please provide your thoughts/help.

Øystein

unread,
Oct 15, 2013, 2:51:58 AM10/15/13
to webd...@googlegroups.com

Hi!

 

The xpath to the span with the text is //span[Text()=’YES’]

 

The xpath to the a is: div/h2/a

 

Now put it together: //div[span[Text()=’YES]/h2/a

 

That basically it translates to: Find all the divs with the text = to YES. Then finds the ‘h2’ tag and then the ‘a’ tag

 

You say the list can grow. So if there are more links with Yes I would handle that in code instead of using xpath.  I never use Text() in my xpaths. I always use the IWebElement Text property instead. I find it much more reliable. But that’s just meJ

 

A tip for experimenting with xpath is to use the firebug addon firepath. (it is not updated anymore but still a excellent tool to use for xpath etc) I use it daily in my work.

 

Let me know if you are stuck.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.

unni

unread,
Oct 15, 2013, 3:31:01 AM10/15/13
to webd...@googlegroups.com
Thank you Austin, It worked.
Reply all
Reply to author
Forward
0 new messages