Webdriver save image to a folder from a website
2 min readSep 4, 2019
Java code-
import javax.imageio.ImageIO;import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;public class WebdriverSaveImage {
WebDriver driver;
@BeforeClass()
public void setUp() throws IOException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\Jar_files\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// To start Chrome in Maximized browser window
options.addArguments("start-maximized");
// To remove Chrome is being controlled by automated test software
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);driver.manage().window().maximize();
}@Test(priority = 1)
public void navigateToWebSite() throws IOException {
// Navigate to the URL
driver.get("https://www.google.com/");
//Ge the location of the image.
WebElement googleLogo = driver.findElement(By.xpath("//img[@id='hplogo']"));
//Get the src attribute value , it has the path to the image src="/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
String imagePath = googleLogo.getAttribute("src");
//Assign imagePath to imageURL
URL imageURL = new URL(imagePath);
//Reading the image using BufferedImage and save it to saveImage
BufferedImage saveImage = ImageIO.read(imageURL);
//Write the image to location given in worksapce with given type and name
ImageIO.write(saveImage, "png", new File("resources\\Inmg_google_logo.png"));
}
@Test(priority = 2)
public void getScreenShotOfAnElement() throws IOException {
// Navigate to the URL
driver.get("https://stackoverflow.com/users/login");
WebElement ele = driver.findElement(By.xpath("//input[@id='email']"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);// Get the location of element on the page
Point point = ele.getLocation();// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);// Copy the element screenshot to disk
File screenshotLocation = new File("resources\\Email.png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
@AfterClass()
public void shutDown() {
driver.close();
driver.quit();
try {
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("***************End of testing shutdown happend**************************");
}
}