User Agents

Web browsers and everyday web tech, explained

How to View a Headless Selenium Browser Session

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Headless browsers run without a visible window, which is great for automation but makes debugging tricky. You can still view and record what the browser is doing by capturing screenshots, recording video, or using a remote debugging interface. This guide shows you several practical methods to see and render a headless Selenium session, so you can verify your scripts are working correctly.

What You'll Need

  • Python 3.6+ installed on your system.
  • Selenium WebDriver installed (via pip: pip install selenium).
  • A WebDriver binary for your browser (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox).
  • Optional: ffmpeg for video recording, or pillow for image manipulation.

Step-by-Step

  1. Set up a basic headless browser. Start with a minimal Selenium script that launches a headless browser. For Chrome, use options like --headless=new and --window-size=1920,1080 to ensure a realistic viewport.
  2. Take screenshots at key points. Use driver.save_screenshot('screenshot.png') or driver.get_screenshot_as_file() to capture the current state. Place these calls after page loads or after actions that change the UI.
  3. Capture full-page screenshots. For long pages, use driver.get_screenshot_as_png() and stitch images together, or use a library like selenium-screen-shot that handles full-page captures automatically.
  4. Record a video of the session. Use a tool like ffmpeg to record the screen area where the browser window would be. Alternatively, use a Python library like pyautogui to capture the screen at intervals and combine frames into a video.
  5. Use remote debugging. Launch the browser with --remote-debugging-port=9222 and then connect to it with a debugging client like Chrome DevTools Protocol (CDP). You can then use pychrome or selenium-wire to inspect and even render the page in a separate viewer.
  6. Run the browser in non-headless mode temporarily. If you just need to see what's happening, remove the headless flag and run the script with a visible window. This is the simplest way to debug, but it requires a graphical desktop.
  7. Use a virtual display. On Linux, use xvfb to run the browser in a virtual framebuffer, then capture screenshots or video from that display. This allows headless execution while still being able to view the output.

Common Mistakes

  • Forgetting to set a window size – headless browsers default to 800x600, which may not reflect your target layout.
  • Taking screenshots before the page finishes loading – use explicit waits for elements or time.sleep() as a fallback.
  • Using the wrong WebDriver version – always match your browser and driver versions.
  • Not closing the browser – always call driver.quit() to free resources.

Tips

  • Use --disable-gpu to avoid rendering issues in headless mode.
  • For screenshots, use PNG format to avoid compression artifacts.
  • If recording video, set a fixed frame rate (e.g., 10 fps) to keep file sizes manageable.
  • Use driver.get_screenshot_as_base64() to embed screenshots directly in test reports.

Troubleshooting

If you're not seeing the expected output, check the following:

  • Screenshots are blank or black: Ensure the browser has finished rendering. Try adding a short delay or waiting for a specific element.
  • Video recording is empty: Make sure you're recording the correct display. On Linux, use xvfb and point ffmpeg to the right DISPLAY variable.
  • Remote debugging not working: Verify the port is open and that you're using the correct WebSocket URL from http://localhost:9222/json.
  • Browser crashes in headless mode: Update your browser and driver, and try adding --no-sandbox if you're on Linux as root.

Frequently Asked Questions

Can I view a headless session in real-time?

Yes, by using remote debugging and a tool like Chrome DevTools Protocol, you can see the page in a separate browser tab or a custom viewer.

How do I record a video without a visible window?

Use a virtual display like Xvfb on Linux, or use a screen capture library that records the headless browser's output (e.g., using pyautogui with a virtual display).

What's the easiest way to debug a headless script?

Take screenshots at each step and save the HTML source with driver.page_source to inspect the state.

Can I use Selenium's built-in screenshot feature for full-page captures?

No, Selenium's native screenshot only captures the viewport. Use a library like selenium-screen-shot or manually scroll and stitch.

Does headless mode affect JavaScript execution?

No, headless browsers execute JavaScript the same way as headed browsers, but some features like WebGL may be disabled by default.

Is it possible to interact with the headless session from another program?

Yes, using the remote debugging protocol, you can send commands and receive responses from any program that speaks CDP.