Question

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.

Was it helpful?

Solution

/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>

OTHER TIPS

You can find all links like this:

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

or:

//a[starts-with(@href,'/news/')]/@href
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top