Tuesday, February 12, 2013

Find Links in Webpage by Selenium Webdriver

Simple code to find links in webpage by Selenium Webdriver



public class FindLink {


public static void main(String[] args)throws InterruptedException {
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");

//Find links
List all_links_webpage=driver.findElements(By.tagName("a"));

//Find link count in webpage
System.out.println(( all_links_webpage).size());
for(int i =0; i<(all_links_webpage).size();i++)
 {

//To  show Link text
System.out.println(((WebElement) all_links_webpage.get(i)).getText());

//To show link attribute
System.out.println(((WebElement) all_links_webpage.get(i)).getAttribute("href"));
}
}
}