selenium scripts
You could use the getXPathCount
command to return the number of links on the page, and then loop through them using XPath. A simple example using Java with Selenium RC follows:
int linkCount = selenium.getXpathCount("/descendant::a").intValue();for (int i = 0; i < linkCount; i++) { selenium.click("/descendant::a[" + i + "]"); selenium.waitForPageToLoad("60000"); //ADD YOUR CHECKS HERE selenium.goBack(); selenium.waitForPageToLoad("60000");}
If you're using Selenium 2 or WebDriver, the following should work:
List<WebElement> links = driver.findElements(By.xpath("/descendant::a"));int lSize = links.size();for (int l = 0; l < lSize; l++) { links = driver.findElements(By.xpath("/descendant::a")); WebElement link = links.get(l); link.click(); //ADD YOUR CHECKS HERE driver.navigate().back();}
Hope that helps.
New answer for solving using Selenium IDE:
Note: You'll need to install the Flow Control plugin from https://addons.mozilla.org/en-US/firefox/addon/85794/ (or use the user-extension from http://51elliot.blogspot.com/2008/02/selenium-ide-goto.html)
<tr> <td>storeXpathCount</td> <td>//body/descendant::a</td> <td>linkCount</td></tr><tr> <td>store</td> <td>1</td> <td>link</td></tr><tr> <td>label</td> <td>checkLink</td> <td></td></tr><tr> <td>echo</td> <td>checking link ${link} of ${linkCount}</td> <td></td></tr><tr> <td>clickAndWait</td> <td>//body/descendant::a[${link}]</td> <td></td></tr><!-- ADD YOUR CHECKS HERE --><tr> <td>goBackAndWait</td> <td></td> <td></td></tr><tr> <td>while</td> <td>storedVars['link'] <= storedVars['linkCount']</td> <td></td></tr><tr> <td>storeEval</td> <td>storedVars['link'] = ${link} + 1;</td> <td></td></tr><tr> <td>gotolabel</td> <td>checkLink</td> <td></td></tr><tr> <td>endWhile</td> <td></td> <td></td></tr>
Click on All links on page:
@Test public void test () throws InterruptedException { try { List<WebElement> no = driver.findElements(By.tagName("a")); int nooflinks = no.size(); System.out.println(nooflinks); for (WebElement pagelink : no) { no.click(); String linktext = pagelink.getText(); driver.navigate.refresh(); Thread.sleep(1000); System.out.println(linktext); } } catch (Exception e){ System.out.println("error "+e); } }