How to View a Headless Selenium Browser Session
In this article
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:
ffmpegfor video recording, orpillowfor image manipulation.
Step-by-Step
- Set up a basic headless browser. Start with a minimal Selenium script that launches a headless browser. For Chrome, use options like
--headless=newand--window-size=1920,1080to ensure a realistic viewport. - Take screenshots at key points. Use
driver.save_screenshot('screenshot.png')ordriver.get_screenshot_as_file()to capture the current state. Place these calls after page loads or after actions that change the UI. - Capture full-page screenshots. For long pages, use
driver.get_screenshot_as_png()and stitch images together, or use a library likeselenium-screen-shotthat handles full-page captures automatically. - Record a video of the session. Use a tool like
ffmpegto record the screen area where the browser window would be. Alternatively, use a Python library likepyautoguito capture the screen at intervals and combine frames into a video. - Use remote debugging. Launch the browser with
--remote-debugging-port=9222and then connect to it with a debugging client like Chrome DevTools Protocol (CDP). You can then usepychromeorselenium-wireto inspect and even render the page in a separate viewer. - 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.
- Use a virtual display. On Linux, use
xvfbto 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-gputo 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
xvfband 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-sandboxif 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.