Wednesday, May 7, 2014

How to save the images in a newly created folder while execution.

Note- create a folder with name Images and have InputData.xslx and just copy paste the below code and run it

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Kalai {
public static void main(String[] args) throws InvalidFormatException, IOException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.urbanspoon.com/c/1/Seattle-restaurants.html");
driver.findElement(By.partialLinkText("City of Seattle")).click();
List<WebElement> nameOfRestros = driver.findElements(By.className("resto_name"));
int numOfRestros = nameOfRestros.size();
for(int i=0; i<numOfRestros; i++){
nameOfRestros.get(i).click();
mainProc(driver,i);
driver.get("http://www.urbanspoon.com/.../City-of-Seattle-restaurants");
nameOfRestros = driver.findElements(By.className("resto_name"));
}

driver.close();
}
public static void mainProc(WebDriver driver, int rowNum) throws InvalidFormatException, IOException{
String nameOfResto = driver.findElement(By.xpath("//h1[@itemprop='name']")).getText().trim();

boolean create = true;
writeData(rowNum, 0, nameOfResto, create);
create = false;
String phone = driver.findElement(By.xpath("//a[@class='phone tel no-link']")).getText().trim();

writeData(rowNum, 1, phone, create);
String address = driver.findElement(By.className("tags")).getText().trim();

writeData(rowNum, 2, address, create);

driver.findElement(By.linkText("View menu")).click();

List<WebElement> img = driver.findElements(By.xpath("//div[@class='list']//div[@class='image']//img"));

System.out.println(img.size());
for(int i=0; i<img.size(); i++){
String imgSrc = img.get(i).getAttribute("src");

WebElement dishEle = img.get(i).findElement(By.xpath("..")).findElement(By.xpath("..")).findElement(By.xpath("..")).findElement(By.xpath(".."));
String dishName = dishEle.findElement(By.tagName("h3")).getText();
File folder = new File("./Images/"+nameOfResto);
folder.mkdir();

String folderName = folder.getAbsolutePath();
pasteImg(folderName, imgSrc, dishName, i);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,10000)", "");
}
}
public static void writeData(int rowNum, int cellNum, String value, boolean create) throws InvalidFormatException, IOException{
FileInputStream fis = new FileInputStream("./InputData.xlsx");
Workbook wb = WorkbookFactory.create(fis);
if(create)
wb.getSheet("Sheet3").createRow(rowNum).createCell(cellNum).setCellValue(value);
else
wb.getSheet("Sheet3").getRow(rowNum).createCell(cellNum).setCellValue(value);
FileOutputStream fos = new FileOutputStream("./InputData.xlsx");
wb.write(fos);
}
public static void pasteImg(String folderName, String imgSrc, String dishName, int i) throws IOException{
URL url = new URL(imgSrc);
BufferedImage image = ImageIO.read(url);
File file = new File(folderName+"/"+dishName+".jpg");
ImageIO.write(image,"jpg", file);
}
}

4 comments:

  1. Hi Sanjay,
    I am manish kumar, i have visited your blogs,it was realy very2 much informatic.
    i would request to you add some example related to framework like: 1. How to create framework structute
    for data driven framework or Hybrid framework. And how do we impliment in our project.
    Thanks.

    ReplyDelete
  2. Thanks Manish.
    Sure, very soon these topics will be here in this blog.

    ReplyDelete
  3. Hi Sanjay,
    Thank you very much for this brilliant blog. I have a question: I added poi-3.10-FINAL-20140208.jar to be able to import Workbook and WorkbookFactory. I did Workbook but not WorkbookFactory and couldn't find the reason why.

    ReplyDelete