How do I take a screenshot of the current page with Selenium WebDriver?

To take a screenshot of the current page using Selenium WebDriver, you can use the save_screenshot method of the WebDriver object in Python. For other programming languages like Java and JavaScript with NodeJS, similar methods are available.

Here are examples in Python, Java, and JavaScript (NodeJS) on how to take a screenshot of the current page using Selenium WebDriver.

Python

from selenium import webdriver

# Assuming you have the correct WebDriver for the browser you are using
driver = webdriver.Chrome()

# Opening a webpage
driver.get("http://www.example.com")

# Taking a screenshot and saving it to a file
driver.save_screenshot("screenshot.png")

# Closing the browser
driver.quit()

Java

In Java, you will need to cast the WebDriver instance to TakesScreenshot to access the getScreenshotAs method.

import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class ScreenshotExample {
    public static void main(String[] args) throws IOException {
        WebDriver driver = new ChromeDriver();

        // Open a webpage
        driver.get("http://www.example.com");

        // Take a screenshot and save it to a file
        File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("screenshot.png"));

        // Close the browser
        driver.quit();
    }
}

Make sure to add the necessary imports and to handle exceptions properly. You may also need to add the selenium-java and commons-io libraries to your project's dependencies.

JavaScript (NodeJS)

With NodeJS, you can use the writeFile function from the fs module to save the screenshot.

const { Builder } = require('selenium-webdriver');
const fs = require('fs');

(async function takeScreenshot() {
    let driver = await new Builder().forBrowser('chrome').build();

    try {
        // Open a webpage
        await driver.get('http://www.example.com');

        // Take a screenshot and save it to a file
        const screenshot = await driver.takeScreenshot();
        fs.writeFileSync('screenshot.png', screenshot, 'base64');
    } finally {
        // Close the browser
        await driver.quit();
    }
})();

Before running the NodeJS code, ensure you have installed the selenium-webdriver package using npm:

npm install selenium-webdriver

Remember, to run these examples, you need to have the appropriate WebDriver executable (e.g., chromedriver for Chrome, geckodriver for Firefox) installed and available in your system's PATH, or specify its location in your code. Also, make sure you have the Selenium library installed for the language you're using.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon