User Agents

Web browsers and everyday web tech, explained

How to Record Selenium WebDriver Browser Sessions

· 5 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

Recording a Selenium WebDriver browser session lets you capture a video of your automated tests or scripts as they run. This is useful for debugging failures, sharing demonstrations, or documenting test flows. To record, you typically use a screen recorder tool or a library like Monte Media Library (Java) or ffmpeg. This guide covers practical steps using free, cross-platform tools.

What You'll Need

  • A working Selenium WebDriver setup (Java, Python, or your preferred language).
  • A screen recording tool: OBS Studio (free, cross-platform) or ffmpeg (command-line).
  • For Java, the Monte Media Library (add as a Maven dependency or download the JAR).
  • For Python, you can use pyautogui or opencv-python to capture frames.

Step-by-Step

  1. Set up your Selenium test script – Write your test or automation script as you normally would. Ensure it runs in a visible browser window (not headless) unless your recording tool can capture a virtual display.
  2. Choose a recording method – Decide between recording the entire screen or just the browser window. For most cases, recording the screen is simplest. For Java, you can integrate Monte Media Library directly into your test code to record only the browser window.
  3. Option A: Record with OBS Studio – Download and install OBS Studio. Create a new scene and add a “Display Capture” or “Window Capture” source, selecting your browser window. Set the output format to MP4 and start recording before running your Selenium script. Stop recording after the script finishes.
  4. Option B: Record with ffmpeg – Open a terminal and run a command like ffmpeg -f x11grab -i :0.0 -f pulse -i default -c:v libx264 -c:a aac output.mp4 (Linux) or ffmpeg -f gdigrab -i desktop -framerate 30 output.mp4 (Windows). Start the command in the background, run your Selenium script, then stop ffmpeg with Ctrl+C.
  5. Option C: Record programmatically with Monte Media Library (Java) – Add the dependency to your pom.xml or classpath. In your test, create a ScreenRecorder object, start it before your Selenium actions, and stop it after. Save the recording as a video file.
  6. Run your Selenium script – Execute your test. Make sure the browser window stays visible and is not minimized, as some recording methods may not capture minimized windows.
  7. Stop the recording – After the script completes, stop the screen recorder or the programmatic recording. Save the video file to a desired location.
  8. Review and share – Play the video to verify it captured the session correctly. You can then share it with your team or attach it to bug reports.

Common Mistakes

  • Recording the wrong window – If you use window capture, ensure you select the correct browser window. If the browser opens a new window, the capture may stop working.
  • Minimizing the browser – Some operating systems pause rendering for minimized windows, resulting in a blank or frozen video.
  • Forgetting to stop the recording – This leads to huge files or corrupted videos. Always stop the recording immediately after the test.
  • Using headless mode – Headless browsers have no visible window, so screen recording will capture nothing. Use a headed browser for recording.
  • Not synchronizing start/stop – Starting the recorder after the test begins or stopping it early will miss parts of the session.

Tips

  • Use a fixed browser window size (e.g., 1280x720) to ensure consistent video quality and aspect ratio.
  • Add a small delay (2-3 seconds) after starting the recorder before running the test to ensure the capture begins cleanly.
  • For long tests, consider recording in segments and concatenating them later to avoid file size issues.
  • If using ffmpeg, set a reasonable frame rate (15-30 fps) to balance file size and smoothness.
  • For Java, Monte Media Library can record only the browser window, which reduces file size and focuses on the relevant area.
  • Use a virtual display (e.g., Xvfb on Linux) if you need to run tests headlessly but still want to record – you can record the virtual display.

Troubleshooting

  • Video is black or empty – Check if the browser window is minimized or covered by other windows. Ensure your screen capture source is set correctly. Try using “Display Capture” instead of “Window Capture”.
  • Recording stops when browser window moves – Window capture may lose track if the window moves or resizes. Keep the window stationary or use display capture.
  • File size is too large – Reduce the frame rate, lower the resolution, or use a more efficient codec like H.264. For ffmpeg, add -crf 28 to balance quality and size.
  • Monte Media Library throws an exception – Ensure you have the correct dependencies (e.g., com.github.stephenc.monte:monte-screen-recorder). Check the Java version compatibility.
  • ffmpeg command not found – Install ffmpeg via your package manager (e.g., sudo apt install ffmpeg on Ubuntu, or download from ffmpeg.org for Windows).
  • Recording is choppy or laggy – Close unnecessary applications, reduce the recording resolution, or increase the bitrate. For OBS, try lowering the FPS to 15.

Frequently Asked Questions

Can I record a Selenium session without installing extra software?

Yes, on Windows you can use the built-in Game Bar (Win+G) to record the screen, and on macOS you can use QuickTime Player's screen recording feature. However, these are manual and may not be suitable for automated integration.

How do I record only the browser window, not the whole screen?

Use OBS Studio with a “Window Capture” source, or use Monte Media Library in Java which can capture a specific screen region. For ffmpeg, you can specify a region using the -video_size and -offset_x/-offset_y options.

Can I record in headless mode?

No, because headless browsers have no visible window to capture. You can use a virtual display like Xvfb on Linux to run a headed browser in a virtual screen and record that display.

Is it possible to record video directly from Selenium WebDriver?

Not natively. Selenium does not have a built-in video recording feature. You must use an external tool or library to capture the screen.

What is the best format for recording Selenium sessions?

MP4 with H.264 video codec is widely compatible and compresses well. For lossless quality, use AVI or MKV with a lossless codec, but be aware of large file sizes.

Can I record audio along with the video?

Yes, most screen recorders can capture system audio or microphone. For ffmpeg, add an audio input device. However, for most Selenium tests, audio is not necessary.