Wednesday, October 29, 2014

Chapter 22- Find all Links

Find all Links

Testers might be in a situation to grep all the links on website. We can easily do so by finding all elements with Tag Name "a" as we know that for any link reference in HTML we need to use "a" (anchor) tag.

Example

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class getalllinks
{
  public static void main(String[] args)
  {
 WebDriver driver = new FirefoxDriver();
 driver.navigate().to("http://www.calculator.net");
 java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
 System.out.println("Number of Links in the Page is " + links.size());
 for (int i = 1; i<=links.size(); i=i+1)
 {
   System.out.println("Name of Link# " + i - + links.get(i).getText());
 }    
  }
}

Output

The Output of the script would be thrown to console as shown below. Though there are 65 links only partial output is shown below.
Selenium IDE 91

No comments: