2

I want to use selenium scripts to click on a bunch of links on my webpage one by one, each click results in a page refresh. However selenium doesn't support css pseudo class like :visited, so I can't distinguish the ones that are already clicked from the ones that I want to click on next. Is there a way to solve my problem?

here's my code: http://pastebin.com/z0uRTHHp

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I'm doing this in javascript and html test cases in Firefox addon, please see the code link in my question. I'm not sure how to fit your concept into the frame(html and javascript test case) I have, can you help and maybe provide some sample code?
Added new answer for solving through Selenium IDE
0

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'] &lt;= 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>

2 Comments

The following xpath with clickAndWait doesn't work in selenium, any idea? clickAndWait //a[text()='Test'][${link}]
It may be because of a subtlety in XPath where //a[1] will select all descendant a elements that are the first para children of their parents, and not the first a element in the entire document. It might work better for you to use something like //body/descendant::a[1] or anchor it to an element with an id like id('myLinks')/descendant::a[1]. Not that for the last example you would need to proceed the locator with xpath=.
0

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);
        }
     } 

Comments

0

first you need to get all links in your web page :

List<WebElement> links= driver.findElements(By.Tag('a'));

second thing you need to iterate one by one with click on particular link and as you mentioned after click your page is going to refresh ,so in that case you need to wait for your next link to fully click able :

for(WebElement link: links) 
{
//wait for element to clickable
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(link);
link.click();
}

and if you want to distinguish that what link is already been clicked so may be your link text color will fade or change after click .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.