- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save a canvas as PNG in Selenium?
We can save a canvas as png in Selenium. We shall first identify the canvas with the help of any locators like xpath, css, and so on. Then obtain the value of src attribute with the getAttribute method.
We shall build an URL in java with the class URL and have a BufferedImage via ImageIOclass. Then utilize the same class to save the canvas with the png extension in a location.
Let us try to save the below image within the project folder.
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; public class SaveCanvas{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // identify canvas WebElement l = driver.findElement(By.xpath("//img[@title='Tutorialspoint']")); // get src value of element String v = l.getAttribute("src"); // creating URL URL i = new URL(v); //having BufferedImage with ImageIO class BufferedImage s = ImageIO.read(i); // save image in location with .png extension ImageIO.write(s, "png", new File("Logo.png")); driver.close(); } }
Output
Logo.png file gets created within the project folder.
- Related Articles
- How to capture HTML Canvas as gif/jpg/png/pdf with JavaScript?
- How to save HTML Canvas as an Image with canvas.toDataURL()?
- HTML5 Canvas to PNG File
- How to save as PDF on Chrome using Selenium
- How to draw a png image on a Python tkinter canvas?
- How to save DIV as Image with HTM5 canvas to image with the extension?
- How to save canvas data to file in HTML5?
- How to save HTML5 canvas data to file?
- How to extract text from a web page using Selenium and save it as a text file?
- How to save and restore the HTML5 Canvas states?
- How to save a vector in R as CSV file?
- How to save the str output as a string in R?
- Save a Web Page with Python Selenium
- How to save and load cookies using Python Selenium WebDriver?
- How to save a matrix as CSV file using R?

Advertisements