How to save mobile screenshot using Headless Google Chrome with Selenium

2 min read 06-10-2024
How to save mobile screenshot using Headless Google Chrome with Selenium


Capturing Mobile Screenshots with Headless Chrome and Selenium: A Step-by-Step Guide

Problem: You need to take screenshots of a website on different mobile devices, but manually testing on each device can be tedious and time-consuming.

Solution: Utilize Headless Chrome with Selenium to automate the process and capture screenshots efficiently.

The Challenge of Mobile Testing

Testing websites on various mobile devices is crucial for ensuring optimal user experience. However, manually testing on each device is impractical. This is where headless Chrome and Selenium come in.

Setting Up Your Tools

Prerequisites:

Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Set up options for Headless Chrome and mobile emulation
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

# Set device capabilities (replace with your desired device)
mobile_emulation = {"deviceName": "Pixel 2"}
options.add_experimental_option("mobileEmulation", mobile_emulation)

# Launch Chrome driver
driver = webdriver.Chrome(options=options)

# Load the desired website
driver.get("https://www.example.com")

# Take a screenshot
driver.save_screenshot("screenshot.png")

# Quit the driver
driver.quit()

Explanation:

  1. Import Libraries: Import the webdriver module from Selenium and Options from webdriver.chrome.options to configure Chrome.
  2. Headless Chrome Configuration:
    • options.add_argument("--headless"): Runs Chrome in headless mode, meaning it won't open a visible browser window.
    • options.add_argument("--no-sandbox"): Disables sandboxing (often required for Docker containers).
    • options.add_argument("--disable-dev-shm-usage"): Disables the shared memory file system (also helpful in containers).
  3. Mobile Device Emulation:
    • mobile_emulation = {"deviceName": "Pixel 2"}: Creates a dictionary specifying the desired device. Replace Pixel 2 with your desired mobile device.
    • options.add_experimental_option("mobileEmulation", mobile_emulation): Applies the mobile emulation settings.
  4. Launch Chrome Driver:
    • driver = webdriver.Chrome(options=options): Initializes the Chrome driver with the configured options.
  5. Navigate to the Website:
    • driver.get("https://www.example.com"): Loads the target website.
  6. Take a Screenshot:
    • driver.save_screenshot("screenshot.png"): Captures a screenshot of the current webpage and saves it as "screenshot.png".
  7. Quit the Driver:
    • driver.quit(): Closes the Chrome browser instance.

Benefits of Using Headless Chrome and Selenium

  • Automation: Saves significant time and effort by automating the screenshot process.
  • Consistency: Ensures consistent results across different devices.
  • Scalability: Easily integrates into larger testing frameworks.
  • Flexibility: Allows you to test on various devices and screen sizes.

Example: Capturing Screenshots for Different Devices

To capture screenshots for multiple devices, simply modify the mobile_emulation dictionary with different device names. Here's an example:

# Example of capturing screenshots for different devices
device_list = [
    {"deviceName": "Pixel 2"},
    {"deviceName": "iPhone X"},
    {"deviceName": "Galaxy S9"},
]

for device in device_list:
    options.add_experimental_option("mobileEmulation", device)
    driver = webdriver.Chrome(options=options)
    driver.get("https://www.example.com")
    driver.save_screenshot(f"screenshot_{device['deviceName']}.png")
    driver.quit()

This loop iterates through the device_list, captures screenshots for each device, and saves them with names reflecting the device model.

Conclusion

Headless Chrome and Selenium offer a powerful solution for capturing mobile screenshots efficiently. This technique saves time, ensures consistency, and enables you to test your website on various devices with ease. By leveraging this automation, you can optimize your website for different mobile platforms and provide a seamless user experience.