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 in Selenium by Tejasvi
45 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 by Anvi
• 13,240 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 in Selenium by Abha
• 24,120 points
62 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 in Selenium by Abha
• 24,120 points
201 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 in Selenium by Abha
• 24,120 points
123 views
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,090 points
739 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
989 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
704 views
0 votes
1 answer
0 votes
1 answer