Wednesday, 7 December 2016

fetching link from webpage and writing in excel file Method 2(best case)

steps:
1.create maven project
2.attach selenium and chrome drivers
3.set dependency in pom file for Apache POI
4.attach one excel file in the project

file name :NewLinkWritter.java


package excelExportAndFileIO;

import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;



import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewLinkWritter {



public static void main(String...strings )  throws IOException{
NewLinkWritter obj=new NewLinkWritter();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();

driver.get("http://vishnuyardinichari.blogspot.in/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");

System.out.println("writting links");

String filePath= System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
String sheetName="sheet1";
File file =    new File(filePath);
FileInputStream inputStream = new FileInputStream(filePath);
Workbook guru99Workbook = new XSSFWorkbook(inputStream);
Sheet sheet = guru99Workbook.getSheet(sheetName);

int rowCount=0;
int columnCount=0;

for(WebElement link:links)
{
Row newRow = sheet.createRow(rowCount++);
Cell cell = newRow.createCell(columnCount);
cell.setCellValue(link.getAttribute("href"));
}
System.out.println("URL-----"+links.size());

FileOutputStream outputStream = new FileOutputStream(file);
guru99Workbook.write(outputStream);
//guru99Workbook.close();
//guru99Workbook.close();
driver.close();
//sheet1.close();
}}


No comments:

Post a Comment