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.edureka.co/");
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();
}
}