Saturday, 18 February 2012

Extract all links from a webpage using webdriver for selenium automation testing


import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class dadabhagwan_LearnSeleniumWithJasmine {

public static void main(String[] args) throws InterruptedException {

WebDriver myDriver = new FirefoxDriver();
myDriver.get("http://satsang.dadabhagwan.org/");

/*Extract all links from the webpage using selenium webdriver*/
List all_links_webpage = myDriver.findElements(By.tagName("a"));

/*Print total no of links on the webpage*/
System.out.println("Print total no of links on the webpage---------------------------------------------");
System.out.println(all_links_webpage.size());

/*Print text of all links*/
System.out.println("Print text of all links------------------------------------------------------------");
for(int i=0;i
System.out.println(all_links_webpage.get(i).getText());
}

/*Print Links*/
System.out.println("Print Links------------------------------------------------------------------------");
for(int i=0;i
System.out.println(all_links_webpage.get(i).getAttribute("href"));
}



/*Extract all links from the part of the webpage using selenium webdriver*/
System.out.println("Extract all links from the part of the webpage using selenium webdriver-----------------------------");
List myList = myDriver.findElement(By.xpath("//*[@id='Flot']")).findElements(By.tagName("a"));

System.out.println("total no links on specific part of webpage---------------------------------------------------");
System.out.println(myList.size());

System.out.println("Text of the link for specific part of webpage--------------------------------------------------");
for(int i =0; i< myList.size();i++){
System.out.println(myList.get(i).getText());

}

}

}