Thursday 23 August 2012

Learn selenium webdriver by example

1. Download Eclipse http://www.eclipse.org/downloads/
2. make new java project http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-3.htm
3. Create new class file with main
4. copy paste following code and enjoy 


public class ScrapNaukri {


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

WebDriver driver = new FirefoxDriver();
driver.get("http://www.naukri.com");


driver.findElement(By.xpath("//*[@id='spanid_farea']/input[@id='farea']")).sendKeys("QA & Testing");

//Enter Location
//driver.findElement(By.xpath("//*[@id='ql']")).sendKeys("pune");

// Click on Search
driver.findElement(By.xpath("//*[@id='search']")).click();

//Xpaths
String Start_xpath_searchString = "//*[@id='";
String End_xpath_searchString = "']";


FileWriter fstream = new FileWriter("C:\\outfilename.txt", true);
BufferedWriter out = new BufferedWriter(fstream);


try {
while ((driver.findElement(By.xpath("//*[@id='pageNext']"))) != null) {

//Click on each link of search result
for(int i=1;i<=50;i++){

try{
driver.findElement(By.xpath(Start_xpath_searchString + i +End_xpath_searchString )).click();


// Go to Job Details
Set<String> windowids = driver.getWindowHandles();
Iterator<String> iter= windowids.iterator();


String mainWindowId=iter.next();
iter.next();
String tabbedWindowId=iter.next();

driver.switchTo().window(tabbedWindowId);

try{
driver.findElement(By.xpath("//*[@id='viewBtn']/a")).click();
Thread.sleep(1000);

System.out.println(driver.findElement(By.xpath("//*[@id='contactDet']")).getText());
String test = driver.findElement(By.xpath("//*[@id='contactDet']")).getText();
out.write(test);
out.newLine();

}catch(Exception e){
System.out.println("Skipped-----------------------------" + i);
}

driver.close();
driver.switchTo().window(mainWindowId);
}catch (Exception e) {
System.out.println("No more Page links to click");
}


}

driver.findElement(By.xpath("//*[@id='pageNext']")).click();
}
} catch (Exception e) {
System.out.println("No more Next button");
out.close();
}



driver.quit();
}