import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;// Page Screenshot:
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destFile = new File("screenshots/failure_" + System.currentTimeMillis() + ".png");
Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Element Screenshot (Selenium 4 feature):
WebElement errorCard = driver.findElement(By.id("error-card"));
File elementSrc = errorCard.getScreenshotAs(OutputType.FILE);