How to take screenshot of a frame using Selenium WebDriver?

0 votes
How to take a screenshot using WebDriver of a single frame and not the complete window, within a frameset?

And is it possible to define coordinates of a window for the screenshot or crop the image after taking the screenshot?
Jun 20, 2018 in Selenium by Atul
• 10,240 points
616 views

1 answer to this question.

0 votes

you can use the below code:

import java.awt.image.BufferedImage;
import java.io.File;

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 Screenshot{

    private WebDriver driver;

    public void shootWebElement(WebElement element) throws IOException  {

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

        Point p = element.getLocation();

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

        BufferedImage img = ImageIO.read(take);

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

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

        File f1 = new File("S:\\where\\over\\the\\sky");

        FileUtils.copyFile(take, f1);

    }
}
answered Jun 20, 2018 by Meci Matt
• 9,420 points

Related Questions In Selenium

0 votes
1 answer

How to capture screenshot of a webpage using Selenium Webdriver?

Hi Bindiya, you can use TakesScreenshot interface to capture the ...READ MORE

answered Jul 8 in Selenium by Abha
• 20,110 points
37 views
0 votes
1 answer

How to run a group of test cases using TestNG in Selenium Webdriver?

Hey Kajal, TestNG allows you to perform ordered ...READ MORE

answered Jun 12 in Selenium by Abha
• 20,110 points
66 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,070 points
437 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
787 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
431 views
0 votes
5 answers
0 votes
1 answer