Best Selenium code snippet using org.openqa.selenium.remote.ScreenshotException.getBase64EncodedScreenshot
org.openqa.selenium.remote.ScreenshotException
This exception raised by selenium webdriver when the selenium is unable to take the screenshot or capture the current screen.
Here is an example of writing and printing stacktrace for ScreenshotException.
1private void writeScreenShotCause(Throwable t, Object test, FrameworkMethod method) throws IOException {
2 WebDriver driver = injector.getInstance(WebDriver.class);
3 File file = new File("target/screenshots/" + test.getClass().getName() + "_" + method.getName() + ".png");
4 Throwable cause = t.getCause();
5 boolean fromException = false;
6 while (cause != null) {
7 if (cause instanceof ScreenshotException) {
8 ScreenshotException se = ((ScreenshotException) cause);
9 byte[] screenshot = Base64.getMimeDecoder().decode(se.getBase64EncodedScreenshot());
10 Files.createParentDirs(file);
11 Files.write(screenshot, file);
12 logger.info("Wrote screenshot to " + file.getAbsolutePath());
13 fromException = true;
14 break;
15 } else {
16 cause = cause.getCause();
17 }
18 }
19 if (!fromException) {
20 File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
21 FileUtils.copyFile(scrFile, file);
22 logger.info("Wrote screenshot to " + file.getAbsolutePath());
23 }
24}
Here are code snippets that can help you understand more how developers are using
Source:SlExtendedWebDriver.java
...36 return null;37 }38 public <T> T extractScreenShot(WebDriverException e, OutputType<T> target) {39 if (e.getCause() instanceof ScreenshotException) {40 return target.convertFromBase64Png(((ScreenshotException) e.getCause()).getBase64EncodedScreenshot());41 }42 return null;43 }44 public SlWebElement findElementByCssSelector(String using) {45 WebElement elem = super.findElementByCssSelector(using);46 return (SlWebElement) new SlWebElementImpl(elem);47 /*List<SlWebElementImpl> elements = findElementsBySizzleCss(using);48 if (elements.size() > 0) {49 return (SlWebElementImpl) elements.get(0);50 }51 return null;*/52 }53 public List<SlWebElementImpl> findElementsBySizzleCss(String using) {54 injectSizzleIfNeeded();...
Source:RemoteWebDriverScreenshotTest.java
...41 try {42 driver.findElement(By.id("doesnayexist"));43 fail();44 } catch (NoSuchElementException e) {45 assertTrue(((ScreenshotException) e.getCause()).getBase64EncodedScreenshot().length() > 0);46 }47 }48 @Test49 public void testCanAugmentWebDriverInstanceIfNecessary() {50 if (!(driver instanceof RemoteWebDriver)) {51 System.out.println("Skipping test: driver is not a remote webdriver");52 return;53 }54 RemoteWebDriver remote = (RemoteWebDriver) driver;55 Boolean screenshots = (Boolean) remote.getCapabilities()56 .getCapability(CapabilityType.TAKES_SCREENSHOT);57 if (screenshots == null || !screenshots) {58 System.out.println("Skipping test: remote driver cannot take screenshots");59 }...
Source:RemoteWebDriverTest.java
...30 driver.findElement(By.id("doesnayexist"));31 fail();32 } catch (NoSuchElementException e) {33 assertTrue(e.getCause() instanceof ScreenshotException);34 assertTrue(((ScreenshotException) e.getCause()).getBase64EncodedScreenshot().length() > 0);35 }36 }37 /**38 * Issue 24839 * @see <a href="http://code.google.com/p/webdriver/issues/detail?id=248">Issue 248</a>40 */41 @JavascriptEnabled42 public void testShouldBeAbleToCallIsJavascriptEnabled() {43 assertTrue(((JavascriptExecutor) driver).isJavascriptEnabled());44 }45}...
Source:ScreenshotException.java
...15 super("Screen shot has been taken", cause);16 screenshot = screenGrab;17 }18 19 public String getBase64EncodedScreenshot() {20 return screenshot;21 }22}...
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.OutputType;2import org.openqa.selenium.TakesScreenshot;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.remote.RemoteWebDriver;5import org.openqa.selenium.remote.ScreenshotException;6public class Base64Screenshot {7 public static void main(String[] args) {8 try {9 String base64 = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.BASE64);10 } catch (ScreenshotException se) {11 String base64 = se.getBase64EncodedScreenshot();12 }13 }14}
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.chrome.ChromeDriver;3import org.openqa.selenium.remote.ScreenshotException;4import org.openqa.selenium.remote.RemoteWebDriver;5import org.openqa.selenium.remote.RemoteWebElement;6import java.io.File;7import java.io.FileOutputStream;8import java.io.IOException;9import java.util.Base64;10public class Screenshot {11 public static void main(String[] args) throws IOException {12 System.setProperty("webdriver.chrome.driver", "/Users/username/Downloads/chromedriver");13 WebDriver driver = new ChromeDriver();14 String encodedScreenshot = ((RemoteWebDriver) driver).getScreenshotAs("base64");15 byte[] decodedScreenshot = Base64.getDecoder().decode(encodedScreenshot);16 File f = new File("/Users/username/Downloads/Screenshot.png");17 FileOutputStream fos = new FileOutputStream(f);18 fos.write(decodedScreenshot);19 fos.close();20 }21}22import org.openqa.selenium.WebDriver;23import org.openqa.selenium.chrome.ChromeDriver;24import org.openqa.selenium.remote.RemoteWebDriver;25import org.openqa.selenium.remote.RemoteWebElement;26import java.io.File;27import java.io.FileOutputStream;28import java.io.IOException;29import java.util.Base64;30public class Screenshot {31 public static void main(String[] args) throws IOException {32 System.setProperty("webdriver.chrome.driver", "/Users/username/Downloads/chromedriver");33 WebDriver driver = new ChromeDriver();34 String encodedScreenshot = ((RemoteWebElement) driver).getScreenshot();35 byte[] decodedScreenshot = Base64.getDecoder().decode(encodedScreenshot);36 File f = new File("/Users/username/Downloads/Screenshot.png");37 FileOutputStream fos = new FileOutputStream(f);38 fos.write(decodedScreenshot);39 fos.close();40 }41}42import org.openqa.selenium.WebDriver;43import org.openqa.selenium.chrome.ChromeDriver;44import org.openqa.selenium.remote.RemoteWebDriver;45import org.openqa.selenium.remote.RemoteWebElement;46import java.io.File;47import java.io.FileOutputStream;48import java.io.IOException;49import java.util.Base64;50public class Screenshot {51 public static void main(String[] args) throws IOException {52 System.setProperty("webdriver.chrome.driver", "/Users/username/Downloads/chromedriver");53 WebDriver driver = new ChromeDriver();54 String encodedScreenshot = ((RemoteWebDriver
getBase64EncodedScreenshot
Using AI Code Generation
1package com.seleniumtests;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.remote.ScreenshotException;7import java.io.File;8import java.io.FileOutputStream;9import java.io.IOException;10import java.nio.file.Files;11import java.nio.file.Paths;12import java.util.Base64;13public class ScreenshotExceptionTest {14 public static void main(String[] args) throws IOException {15 WebDriver driver = new ChromeDriver();16 WebElement element = driver.findElement(By.id("gsc-i-id1"));17 element.sendKeys("selenium");18 try {19 element.click();20 } catch (ScreenshotException e) {21 byte[] decodedBytes = Base64.getDecoder().decode(e.getBase64EncodedScreenshot());22 File file = new File("screenshot.png");23 FileOutputStream fileOutputStream = new FileOutputStream(file);24 fileOutputStream.write(decodedBytes);25 fileOutputStream.close();26 }27 driver.quit();28 }29}30package com.seleniumtests;31import org.openqa.selenium.By;32import org.openqa.selenium.WebDriver;33import org.openqa.selenium.WebElement;34import org.openqa.selenium.chrome.ChromeDriver;35import org.openqa.selenium.remote.ScreenshotException;36import java.io.File;37import java.io.FileOutputStream;38import java.io.IOException;39import java.nio.file.Files;40import java.nio.file.Paths;41import java.util.Base64;42public class ScreenshotExceptionTest {43 public static void main(String[] args) throws IOException {44 WebDriver driver = new ChromeDriver();45 WebElement element = driver.findElement(By.id("gsc-i-id1"));46 element.sendKeys("selenium");47 try {48 element.click();49 } catch (ScreenshotException e) {50 String screenshot = e.getBase64EncodedScreenshot();51 System.out.println(screenshot);52 }53 driver.quit();54 }55}56package com.seleniumtests;57import org.openqa.selenium.By;58import org.openqa.selenium.WebDriver;59import org.openqa.selenium.WebElement;60import org.openqa.selenium.chrome.ChromeDriver;61import org.openqa.selenium.remote.ScreenshotException;62import java.io.File;63import java.io.FileOutputStream;
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.remote.ScreenshotException;6import java.io.File;7import java.io.FileOutputStream;8import java.io.IOException;9import java.util.Base64;10public class Base64Screenshot {11 public static void main(String[] args) throws IOException {12 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");13 WebDriver driver = new ChromeDriver();14 driver.manage().window().maximize();15 WebElement searchBox = driver.findElement(By.name("q"));16 searchBox.sendKeys("Selenium");17 searchBox.submit();18 try {19 WebElement result = driver.findElement(By.id("result"));20 } catch (Exception e) {21 String base64String = ((ScreenshotException) e).getBase64EncodedScreenshot();22 byte[] screenshot = Base64.getDecoder().decode(base64String);23 File file = new File("C:\\Users\\user\\Desktop\\Selenium\\Screenshot\\screenshot.png");24 FileOutputStream fileOutputStream = new FileOutputStream(file);25 fileOutputStream.write(screenshot);26 fileOutputStream.close();27 }28 driver.quit();29 }30}
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.remote.ScreenshotException;2import org.openqa.selenium.remote.RemoteWebDriver;3import org.openqa.selenium.remote.RemoteWebElement;4import org.openqa.selenium.remote.ScreenshotException;5import org.openqa.selenium.remote.RemoteWebDriver;6import org.openqa.selenium.remote.RemoteWebElement;7import java.io.File;8import java.io.IOException;9import java.nio.file.Files;10import java.util.Base64;11import java.util.logging.Logger;12public class Base64Screenshot {13 private static final Logger log = Logger.getLogger(Base64Screenshot.class.getName());14 public static void main(String[] args) {15 RemoteWebDriver driver = new RemoteWebDriver();16 RemoteWebElement element = new RemoteWebElement();17 try {18 element.click();19 } catch (Exception e) {20 log.info("Exception occurred: " + e.getMessage());21 if (e instanceof ScreenshotException) {22 ScreenshotException screenshotException = (ScreenshotException) e;23 String base64EncodedScreenshot = screenshotException.getBase64EncodedScreenshot();24 byte[] decodedScreenshot = Base64.getDecoder().decode(base64EncodedScreenshot);25 File screenshot = new File("screenshot.png");26 try {27 Files.write(screenshot.toPath(), decodedScreenshot);28 } catch (IOException ioException) {29 log.info("Exception occurred while writing the screenshot to the file: " + ioException.getMessage());30 }31 }32 }33 }34}
getBase64EncodedScreenshot
Using AI Code Generation
1public static String getBase64EncodedScreenshotException(Throwable e) {2 String base64EncodedScreenshot = null;3 while (e != null) {4 if (e instanceof ScreenshotException) {5 base64EncodedScreenshot = ((ScreenshotException) e).getBase64EncodedScreenshot();6 break;7 }8 e = e.getCause();9 }10 return base64EncodedScreenshot;11}12public static byte[] getScreenshotAsBytes(Throwable e) {13 String base64EncodedScreenshot = getBase64EncodedScreenshotException(e);14 if (base64EncodedScreenshot == null) {15 return null;16 }17 return Base64.getDecoder().decode(base64EncodedScreenshot);18}19public static File getScreenshotAsFile(Throwable e) {20 byte[] screenshotAsBytes = getScreenshotAsBytes(e);21 if (screenshotAsBytes == null) {22 return null;23 }24 File file = null;25 try {26 file = File.createTempFile("screenshot", ".png");27 Files.write(file.toPath(), screenshotAsBytes);28 } catch (IOException e1) {29 e1.printStackTrace();30 }31 return file;32}33public static void getScreenshotAsFile(Throwable e, File file) {34 byte[] screenshotAsBytes = getScreenshotAsBytes(e);35 if (screenshotAsBytes == null) {36 return;37 }38 try {39 Files.write(file.toPath(), screenshotAsBytes);40 } catch (IOException e1) {41 e1.printStackTrace();42 }43}44public static void getScreenshotAsFile(Throwable e, String filePath) {45 getScreenshotAsFile(e, new File(filePath));46}47public static void getScreenshotAsFile(Throwable e, String fileName, String fileExtension) {48 getScreenshotAsFile(e, new File(fileName + "." + fileExtension));49}50public static void getScreenshotAsFile(Throwable e
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.remote.ScreenshotException;6import java.io.FileOutputStream;7import java.io.IOException;8import java.util.Base64;9public class Base64Screenshot {10 public static void main(String[] args) throws IOException {11 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver_win32\\chromedriver.exe");12 WebDriver driver = new ChromeDriver();13 WebElement searchBox = driver.findElement(By.name("q"));14 searchBox.sendKeys("Selenium");15 searchBox.submit();16 try {17 Thread.sleep(5000);18 } catch (InterruptedException e) {19 e.printStackTrace();20 }21 driver.quit();22 }23}
getBase64EncodedScreenshot
Using AI Code Generation
1package com.automation;2import java.io.File;3import java.io.IOException;4import java.util.Base64;5import java.util.concurrent.TimeUnit;6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;10import org.openqa.selenium.remote.RemoteWebDriver;11import org.openqa.selenium.remote.ScreenshotException;12import org.openqa.selenium.support.ui.ExpectedConditions;13import org.openqa.selenium.support.ui.WebDriverWait;14public class ScreenshotExceptionExample {15 public static void main(String[] args) throws InterruptedException {16 System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Downloads\\chromedriver_win32\\chromedriver.exe");17 WebDriver driver = new ChromeDriver();18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);19 driver.manage().window().maximize();20 WebElement element = driver.findElement(By.name("q"));21 element.sendKeys("Selenium");22 element.submit();23 WebDriverWait wait = new WebDriverWait(driver, 10);24 wait.until(ExpectedConditions.titleContains("Selenium"));25 System.out.println(driver.getTitle());26 String base64 = ((RemoteWebDriver) driver).getScreenshotAs("base64");27 System.out.println(base64);28 try {29 byte[] decodedBytes = Base64.getDecoder().decode(base64);30 File file = new File("C:\\Users\\admin\\Desktop\\Selenium\\Selenium\\screenshot\\base64.png");31 org.apache.commons.io.FileUtils.writeByteArrayToFile(file, decodedBytes);32 } catch (IOException e) {33 e.printStackTrace();34 }35 driver.quit();36 }37}38 private static final Logger log = Logger.getLogger(Base64Screenshot.class.getName());39 public static void main(String[] args) {40 RemoteWebDriver driver = new RemoteWebDriver();41 RemoteWebElement element = new RemoteWebElement();42 try {43 element.click();44 } catch (Exception e) {
getBase64EncodedScreenshot
Using AI Code Generation
1package com.automation;2import java.io.File;3import java.io.IOException;4import java.util.Base64;5import java.util.concurrent.TimeUnit;6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement; log.info("Exception occurred: " + e.getMessage());9import org.openqa.selenium.chrome.ChromeDriver; if (e instanceof ScreenshotException) {10import org.openqa.selenium.remote.RemoteWebDriver;11import org.openqa.selenium.remote.ScreenshotException;12import org.openqa.selenium.support.ui.ExpectedConditions;13import org.openqa.selenium.support.ui.WebDriverWait;14public class ScreenshotExceptionExample {15 public static void main(String ] args) throws InterruptedException {16 System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Downloads\\chromedriver_win32\\chromedriver.exe");17 WebDriver driver = new ChromeDriver();18 driver.manage().timeouts().implicitlyWait( 0, TimeUnit.SECONDS);19 driver.manage().window().maximize();20 WebElement element = driver.findElement(By.name("q"));21 element.sendKeys("Selenium");22 element.submit();23 WebDriverWait wait = new WebDriverWait(driver, 10);24 wait.until(ExpectedConditions.titleContains("Selenium"));25 System.out.println(driver.getTitle());26 String base64 = ((RemoteWebDriver) driver).getScreenshotAs("base64");27 System.out.println(base64);28 try {29 byte[ decodedBytes = Base64.getDecoder().decode(base64);30 File file = new File("C:\\Users\\admin\\Desktop\\Selenium\\Selenium\\screenshot\\base64.png");31 org.apache.commons.io.FileUtils.writeByteArrayToFile(file, decodedBytes);32 } catch (IOException e) {33 e.printStackTrace();34 }35 driver.quit();36 }37}38How to ScreenshotException screenshotException = (ScreenshotException) e;39 String base64EncodedScreenshot = screenshotException.getBase64EncodedScreenshot();40 byte[] decodedScreenshot = Base64.getDecoder().decode(base64EncodedScreenshot);41 File screenshot = new File("screenshot.png");42 try {43 Files.write(screenshot.toPath(), decodedScreenshot);44 } catch (IOException ioException) {45 log.info("Exception occurred while writing the screenshot to the file: " + ioException.getMessage());46 }47 }48 }49 }50}
getBase64EncodedScreenshot
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.remote.ScreenshotException;6import java.io.FileOutputStream;7import java.io.IOException;8import java.util.Base64;9public class Base64Screenshot {10 public static void main(String[] args) throws IOException {11 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver_win32\\chromedriver.exe");12 WebDriver driver = new ChromeDriver();13 WebElement searchBox = driver.findElement(By.name("q"));14 searchBox.sendKeys("Selenium");15 searchBox.submit();16 try {17 Thread.sleep(5000);18 } catch (InterruptedException e) {19 e.printStackTrace();20 }21 driver.quit();22 }23}
getBase64EncodedScreenshot
Using AI Code Generation
1package com.automation;2import java.io.File;3import java.io.IOException;4import java.util.Base64;5import java.util.concurrent.TimeUnit;6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;10import org.openqa.selenium.remote.RemoteWebDriver;11import org.openqa.selenium.remote.ScreenshotException;12import org.openqa.selenium.support.ui.ExpectedConditions;13import org.openqa.selenium.support.ui.WebDriverWait;14public class ScreenshotExceptionExample {15 public static void main(String[] args) throws InterruptedException {16 System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Downloads\\chromedriver_win32\\chromedriver.exe");17 WebDriver driver = new ChromeDriver();18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);19 driver.manage().window().maximize();20 WebElement element = driver.findElement(By.name("q"));21 element.sendKeys("Selenium");22 element.submit();23 WebDriverWait wait = new WebDriverWait(driver, 10);24 wait.until(ExpectedConditions.titleContains("Selenium"));25 System.out.println(driver.getTitle());26 String base64 = ((RemoteWebDriver) driver).getScreenshotAs("base64");27 System.out.println(base64);28 try {29 byte[] decodedBytes = Base64.getDecoder().decode(base64);30 File file = new File("C:\\Users\\admin\\Desktop\\Selenium\\Selenium\\screenshot\\base64.png");31 org.apache.commons.io.FileUtils.writeByteArrayToFile(file, decodedBytes);32 } catch (IOException e) {33 e.printStackTrace();34 }35 driver.quit();36 }37}
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!