0

I'm using Greasekit so need this to be in Javascript.

I want to be able to automatically click the link where it says 25 below. However, the link is not always 25, it is sometimes (say) 21 or 26 (depending on how long the newsletter is). What remains constant is that the link is always in the same position / place on the page. It is ALWAYS the one before Next > and after 3

<table class="v_page_nav" border="0" cellpadding="5" cellspacing="0" width="100%">
<tr>
<td width="100%">Showing 1-25 of 610 Results</td>
<td><span style="color: #999999">< Previous</span> | Page:</td>
<td class="on">
<a href="/wine/newsletter?ie=UTF8&amp;page=1" id="pagination-page-1">1</a></td>
<td class="off">
<a href="/wine/newsletter?ie=UTF8&amp;page=2" id="pagination-page-2">2</a></td>
<td class="off">
<a href="/wine/newsletter?ie=UTF8&amp;page=3" id="pagination-page-3">3</a></td>
<td>...</td>
<td>
<a href="/wine/newsletter?ie=UTF8&amp;page=25" id="pagination-page-25">25</a></td>
<td>| <a href="/wine/newsletter?ie=UTF8&amp;page=2" id="pagination-next-link">Next ></a>      </td>
</tr>
</table>

As I was saying... the link is not always 25, it is sometimes (say) 13 or 29 (depending on how long the newsletter is). What remains constant is that the link is always in the same position / place on the page. It is ALWAYS the one before Next > and after 3

Can I, using code, always find and then click on the appropriate link?

If i was instructing human

I would start by saying to find table class v_page_nav Then, scan your eyes right and look for the link Next >. Click the link that immediately precedes it: it will always be a number (and that number will always be higher than the numbers on the left of it).

How do I achieve the same thing in code?

Community
  • 1
  • 1
Elle
  • 375
  • 1
  • 2
  • 12

1 Answers1

1

Get all of the anchors on the page, loop through them, then return the one right before "Next"

function getPreviousLink(){
    var nextLink = document.getElementById('pagination-next-link'); 
    var links = document.getElementsByClassName('v_page_nav')[0].getElementsByTagName("a");

    for(var i=0; i < links.length; i++){
        if(links[i] == nextLink) { return links[i-1]; }
    }
}
radicalpi
  • 907
  • 1
  • 9
  • 29
  • Thanks for the suggestion. I tried the code and it doesn't seem to work, sadly. Please tell me, did you include the instruction to do the click? (If you didn't could you kindly update). – Elle May 20 '13 at 00:40
  • (It is a large web page, so I wonder if it's worth telling the code jump straight to that table class?) – Elle May 20 '13 at 00:44
  • Added code to limit the scope to the table class. To do the actual click is more complicated in pure javascript. See this question for more information: http://stackoverflow.com/questions/143747/is-it-possible-to-trigger-a-links-or-any-elements-click-event-through-javasc – radicalpi May 20 '13 at 01:03
  • Oh, really? i am able to click a button with just one line of code: `if (inPage && el) el.click();` (taken from http://stackoverflow.com/questions/16191804/if-a-button-appears-on-a-page-automatically-click-it) I shall try to get my head around the page you linked to – Elle May 20 '13 at 01:12
  • Okay, so do I add the following to the end: `function fireEvent(element,event) {` (and the lines below it)? – Elle May 20 '13 at 01:15
  • I appended the code to your answer. I'm going to try it out now. – Elle May 20 '13 at 01:22
  • (didn't work) I'll go and re-read all the documentation and links you kindly provided. – Elle May 20 '13 at 01:27
  • el.click() does appear to work in Chrome and Firefox. I was afraid it was IE-specific. Here's a fiddle with the above code and a click event firing. http://jsfiddle.net/mK2d2/ – radicalpi May 20 '13 at 02:59
  • [to anyone] As a twist, is it possible to open this in a new window? (Not essential, I'm just curious to know how). (I tried to google it and all i found was advice to `Link Text` but that doesn't seem to apply here, as it's javascript, not html. – Elle May 20 '13 at 20:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30284/discussion-between-ted-and-chris-hendry) – Elle May 20 '13 at 23:46