Tuesday 7 June 2022

How To Take Partial Screenshot in Selenium | Selenium 4 Features

                   In this post, we will see How To Take Partial Screenshot in Selenium | Selenium 4 Features 


Program Code:

import java.io.File;

import java.io.IOException;


import org.openqa.selenium.By;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


import com.google.common.io.Files;


public class ScreenshotPartial {


public static void main(String[] args) throws IOException {

System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://amazon.in");

driver.manage().window().maximize();

//full screen screenshot

File f=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Files.copy(f,new File("C:\\Users\\parag\\Downloads\\sample\\amazonscreenshot.jpg"));

//partial screen screenshot   Selenium 4 New Feature

WebElement w1=driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));

w1.click();

w1.sendKeys("pen drive");

File f1=w1.getScreenshotAs(OutputType.FILE);

Files.copy(f1,new File("C:\\Users\\parag\\Downloads\\sample\\textbox.jpg"));


//another partial screenshot

WebElement w2=driver.findElement(By.xpath("(//div[@class='a-cardui fluid-fat-image-link fluid-card fluid-fat-image-link'])[1]"));

File f2=w2.getScreenshotAs(OutputType.FILE);

Files.copy(f2,new File("C:\\Users\\parag\\Downloads\\sample\\box.jpg"));

driver.quit();


}


}  


Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=BuLU3-gW7gY

No comments:

Post a Comment