1

Hello I'm new to selinium ide and xpath thing, that's why I need help regarding this one. I need to click some links on a website i can make it work in 1 link I don't know how it will search and click the other link because it has different number and post title. The link look like this one.

http://imageshack.com/a/img27/328/zv17.png

<a href="/sample/15151-hey-you">post</a>
<a href="/sample/142151-im-okay">post</a>
<a href="/sample/512512-thats-fine">post</a>

I use this xpath and it works on first link

//div[@id='main']/ul[2]/li[1][@class='notification-posted']/a[2]

What is the right xpath that will click 1 link and the preceding links

please help me with this one

Edit
Thank you so much your first code it works but not the second one. but every post in ul is important, your code is working on the first post in ul.

<h5>20 seconds ago</h5>
<ul>
    <li class="notification-posted">
        <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
        <a href="/account/54351-wews">wews</a>
        send new
        <a href="/news/53235">post</a> <!--//li[@class='notification-posted'][1]/a[2]-->
    </li>
</ul>
<h5>3 minutes ago</h5>
<ul>
    <li class="notification-posted">
        <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
        <a href="/account/632323-yokol">yokol</a>
        submitted a new
        <a href="/news/253129-loss">post</a> <!--//li[@class='notification-posted'][2]/a[2]-->
    </li>
</ul>
<h5>4 minutes ago</h5>
<ul/>
<h3>6 minutes ago</h3>
<ul/>
<h5>7 minutes ago</h5>
<ul>
    <h2>8 minutes ago</h2>
    <ul />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <li class="notification-posted" />
    <img height="15" alt="" src="/assets/images/icons/notification-posted.png" />
    <a href="/account/153316-problem">hey</a>
    send new
    <a href="/news/25151-helloworld">post</a> <!--***Problem was here***-->
</ul>

that should be

//li[@class='notification-posted'][6]/a[2] 

right? but it parse other link. thank you for your answer.

Junosaur
  • 15
  • 3

2 Answers2

0

/ul[2]... should return the first post of the second ul

Assuming that the number of posts per ul is both variable, and also not important (i.e. you want the posts irrespective of which ul they are in), then you can bypass the ul.

If <li class='notification-posted'> uniquely identifies posts, you can reference the 3 links as follows:

(//li[@class='notification-posted'])[1]/a[2]
(//li[@class='notification-posted'])[2]/a[2]
(//li[@class='notification-posted'])[3]/a[2]
(// ... )[n]/a[2]

The a[2] is needed because you are after the second link in each instance.

If this isn't selective enough, you can add the filter that the li must also contain an img of notification-posted.png icon is always associated with the hyperlink:

(//li[@class='notification-posted'][img src="/assets/images/icons/notification-posted.png"])[1]//a[2]

and the same for links [2] and [3]

Edit

You can find all links which have the text post with the following xpath:

//a[@href][normalize-space(.)='post']

Tested on the (xhtmlized version of your html) with the following xsl:

<xsl:for-each select="//a[@href][normalize-space(.)='post']">
    <Link>
        <xsl:value-of select="@href"/>
    </Link>
</xsl:for-each>

Gives:

<Link>/news/53235</Link>
<Link>/news/253129-loss</Link>
<Link>/news/25151-helloworld</Link>
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • @user3343430 If all of the links of interest have the word 'post' in their text, you could also try something along the lines of `//a[@href][normalize-text(.)='post']` – StuartLC Feb 24 '14 at 05:27
  • @user3343430 Apologies - I meant `normalize-space`. – StuartLC Feb 24 '14 at 06:03
  • it works only in the first link but not in the next link, how can i make it work on the next links? anyway thank you so much for answering. – Junosaur Feb 24 '14 at 06:07
  • The `//a[][='post']` approach should find all `post` links irrespective of their position. This might also help - http://stackoverflow.com/questions/3674569/how-to-select-specified-node-within-xpath-node-sets-by-index-with-selenium - I had made this error in my initial answer as well - updated. – StuartLC Feb 24 '14 at 06:12
  • that code will find the first link from the top but not the link below :( i can't make it work. i want to make it like this, click the first link on the top then do some clicking and go back to the list and click the second link below. i only had a problem with clicking the list of link. – Junosaur Feb 24 '14 at 06:32
  • Juno, a great tool that you can use would be to install firebug, and then an addon to firebug called firepath. It will allow you to inspect your xpaths rather quickly and accurately. With that being said, pay attention to which frames you may be interacting with as your paths will fail if you do not switch to the proper frame. Cheers – Ben Feb 24 '14 at 19:18
  • hi ben thanks for answering, and thanks for the suggestion i think my title was wrong because i already knew the xpath of each link the problem is how can i click each and every link and how will i write it in selenium ide. do i need to create a new question for this ? :) – Junosaur Feb 25 '14 at 00:30
0

You can find all links like this:

//a[starts-with(@href,'/news/')]

or:

//a[starts-with(@href,'/news/')]/@href
sae
  • 3
  • 2
  • you first answer was trying to click the link that was not on the list the second one has a timeout error. anyway, thanks for helping. – Junosaur Feb 25 '14 at 00:31