Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

Is it possible to take a screenshot with WebDriver of only one frame (not the complete window) within a frameset?

Alternatively, is it possible to define coordinates of a window for the screenshot or crop the image afterwards?

1 Answer

0 votes
by (62.9k points)

I will be providing the Java code which helps to take partial screenshot with selenium webdriver, below is the code:

import javax.imageio.ImageIO;

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;

public class Shooter{

    private WebDriver driver;

    public void shootWebElement(WebElement element) throws IOException  {

 

        File screen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);

        Point p = element.getLocation();

        int width = element.getSize().getWidth();

        int height = element.getSize().getHeight();

        BufferedImage img = ImageIO.read(screen);

        BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width,  height);

        ImageIO.write(dest, "png", screen);

        File f = new File("S:\\path_of_the_file");

        FileUtils.copyFile(screen, f);

    }

}

Browse Categories

...