Selenium scripts

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

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

+2


a source to share


4 answers


New answer to solve the problem using Selenium IDE:

Note. You 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>

      

0


a source


You can use a command getXPathCount

to return the number of links on a page and then loop them through XPath. Below is a simple example using Java with Selenium RC:

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 are using Selenium 2 or WebDriver this 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 it helps.

+3


a source


Click All links on the 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);
        }
     } 

      

0


a source


first you need to get all links in your webpage:

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

      

The second thing you need to iterate over one by one with a click on a specific link, and as you said after the click, your page will refresh, so in this case, you need to wait until your next link is fully accessible:

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 which link has already been clicked, maybe your link text color will fade or change after click.

0


a source







All Articles