How can I capture screenshot of a webpage partially in Selenium

0 votes
How can I capture screenshot of a webpage partially in Selenium?
Jul 8, 2019 in Selenium by Tejasvi
1,016 views

1 answer to this question.

0 votes
Hey Tejasvi, you can capture screenshot of partial webpage using Selenium Webdriver. Following code snippet takes the screenshot of full page and then creates a rectangle to capture a part of that screenshot:
 
public class ScreenshotPartialWebpage {
     WebDriver driver;
     WebElement element ;
 
     @Before
     public void setUp() {
         driver = new FirefoxDriver();
         driver.get("https://www.flipkart.com");
         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
         driver.manage().window().maximize();
     }
    @Test
    public void testApp() throws InterruptedException, IOException {
 
        element = driver.findElement(By.className("puxlXr"));
 
        // Take screen shot of whole web page
        File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
 
        // Calculate the width and height of the element
        Point p = element.getLocation();
        int width = element.getSize().getWidth();
        int height = element.getSize().getHeight();
 
        // Create Rectangle
        Rectangle rect = new Rectangle(width + 600, height + 600);
 
        BufferedImage img = null;
        img = ImageIO.read(screenShot);
 
        //Crop Image of partial web page which includes the "Deals of the day" web element
        BufferedImage bufferedImg = img.getSubimage(p.getX()-300, p.getY()-300, rect.width, rect.height);
 
        // write cropped image into File Object
        ImageIO.write(bufferedImg, "png", screenShot);
 
        //Copy Image into particular directory
        FileUtils.copyFile(screenShot,
                new File("D:/partialWebPage.png"));
    }
     
    @After
    public void tearDown() {
        driver.quit();
    }
 
}
answered Jul 8, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

0 votes
1 answer
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, 2019 in Selenium by Abha
• 28,140 points
1,437 views
+1 vote
1 answer

How can I capture network traffic of a specific page using Selenium?

Hey, to capture network network traffic of ...READ MORE

answered Jul 19, 2019 in Selenium by Abha
• 28,140 points
22,410 views
0 votes
1 answer

How can I get meta-description of a webpage using Selenium Webdriver?

Hey Sonal, to get the meta-description of ...READ MORE

answered Jul 23, 2019 in Selenium by Abha
• 28,140 points
5,307 views
0 votes
2 answers

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,500 points
12,616 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,571 views
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,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,517 views
0 votes
1 answer

How can I perform multiple selection of options in a dropdown using Select class in Selenium?

Hey Priyansh, you can select multiple options ...READ MORE

answered Jul 8, 2019 in Selenium by Anvi
• 14,150 points
2,906 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP