Capture Google Chrome Windows on macOS with Python
In this article
To capture a running Google Chrome window on macOS using Python, the most reliable method involves combining the pyautogui library for screen capturing and the pygetwindow or Quartz framework to identify the specific coordinates of the Chrome window. Because macOS has strict security permissions regarding screen recording and accessibility, you must grant your terminal or IDE permission to record the screen before the script can access the visual data of the browser. This process allows you to automate visual monitoring, create automated testing snapshots, or develop tools that interact with web content that cannot be accessed via standard APIs.
What You'll Need
- A Mac running macOS (Monterey, Ventura, or Sonoma).
- Python 3.8 or higher installed on your system.
- A code editor such as VS Code, PyCharm, or the built-in Terminal.
- Administrative privileges to grant Screen Recording permissions in System Settings.
- The
pyautoguiandPillowlibraries for image manipulation. - The
pyobjc-framework-Quartzlibrary to interface with the macOS window server.
Step-by-Step
- Install Required Dependencies: Open your terminal and install the necessary libraries. You will need
pyautoguifor the actual screenshot functionality andpyobjcto communicate with the macOS Quartz framework, which allows Python to find the exact boundaries of the Chrome window. Run the following command:pip install pyautogui Pillow pyobjc-framework-Quartz. - Configure macOS Security Permissions: macOS blocks applications from recording the screen by default. Go to System Settings > Privacy & Security > Screen Recording. Find your terminal application (e.g., Terminal, iTerm2) or your IDE (e.g., VS Code) and toggle the switch to On. You may need to restart the application for these changes to take effect. Additionally, ensure Accessibility permissions are granted if you plan to move the mouse or type into the window.
- Identify the Chrome Window: Unlike Windows, macOS does not have a simple
getWindowsWithTitlefunction in standard Python libraries. You must use the Quartz framework to iterate through the list of running windows. You will write a function that searches for a window whose name contains "Google Chrome". This function will return the window's X and Y coordinates, as well as its width and height. - Define the Capture Area: Once you have the coordinates (x, y, width, height), you create a bounding box. Note that macOS uses a coordinate system where (0,0) is the top-left of the primary monitor. If Chrome is minimized or hidden behind another window, the capture may result in a screenshot of the window covering it. Ensure Chrome is the active, visible window before triggering the capture.
- Execute the Screenshot: Use
pyautogui.screenshot(region=(x, y, width, height)). Theregionparameter takes a tuple that defines the top-left corner and the dimensions of the area to be captured. This prevents you from taking a full-screen shot and having to crop it manually later. - Save the Output: The result of the screenshot is a Pillow Image object. Use the
.save('chrome_capture.png')method to write the image to your local disk. You can also implement a loop to save frames at specific intervals if you are attempting to record a sequence of events.
Common Mistakes
One of the most frequent errors is forgetting that macOS Retina displays use a scaling factor (usually 2x). When Quartz returns the window coordinates, they are often in "points," but pyautogui or the underlying screen buffer may be working in "pixels." This results in a screenshot that is shifted or only captures a quarter of the intended window. You must multiply the coordinates and dimensions by the scale factor of your display to ensure the capture aligns perfectly with the Chrome window.
Another common mistake is attempting to capture a window that is minimized to the Dock. A minimized window does not have active screen coordinates in the same way a visible window does. If your script returns a null value or an error when searching for the window, verify that Chrome is not minimized. If you need to capture a minimized window, you must first use AppleScript (via the os.system or subprocess module) to bring the application to the foreground.
Developers also often overlook the "Permission Denied" error. If the script runs without crashing but produces a completely black image or a screenshot of only the desktop wallpaper, it is almost always a sign that the Screen Recording permission was not granted to the specific Python interpreter being used. If you are using a virtual environment, the permission must be granted to the parent application launching that environment.
Tips
To make your script more robust, implement a "retry" mechanism. Sometimes the window manager takes a few milliseconds to update the window position after a user moves the browser. Adding a time.sleep(0.5) before the capture can prevent the screenshot from being slightly offset.
If you need to capture a specific tab rather than the whole window, you can use pyautogui to simulate a Cmd + L keystroke to highlight the address bar, then use image recognition (pyautogui.locateOnScreen) to find specific UI elements within the Chrome window to further crop your image to the viewport only.
For high-performance needs, consider using the mss library instead of pyautogui. mss is specifically designed for ultra-fast screenshots and handles multi-monitor setups on macOS more gracefully than many other libraries, reducing the latency between the command and the image capture.
Troubleshooting
Issue: The screenshot is only a partial image of the window.
Check your Retina scaling. Use Quartz.CGDisplayBounds(Quartz.CGMainDisplayID()) to determine the actual pixel width of your screen and compare it to the logical width. If the pixel width is double the logical width, multiply your region coordinates by 2.
Issue: Python cannot find the Chrome window.
Ensure that the window title actually contains "Google Chrome". Some extensions or specific web apps change the window title to the name of the website (e.g., "Inbox - Gmail"). Modify your search logic to look for the application name in the window list rather than just the title string.
Issue: The script crashes with a Quartz error.
This usually happens if the pyobjc framework is not installed correctly or is incompatible with your Python version. Try creating a fresh virtual environment and installing pyobjc-framework-Quartz specifically. Ensure you are not running the script in a restricted sandbox environment.
Issue: Captured images are blurry.
This occurs when the capture resolution does not match the native resolution of the display. Ensure you are capturing the window at its native scale and avoiding any OS-level zoom features (like Accessibility Zoom) which can distort the coordinate mapping.
Frequently Asked Questions
Can I capture a Chrome window that is hidden behind another app?
No, standard screen capture libraries capture the pixels currently rendered on the screen. If another window is covering Chrome, that window will appear in the screenshot. You must bring Chrome to the front using AppleScript or pyautogui clicks before capturing.
Does this method work for Incognito windows?
Yes, because this method captures the screen output rather than interacting with the browser's internal DOM or API, it captures exactly what is visible on the monitor, including Incognito mode.
Will this work on multiple monitors?
Yes, but you must be careful with coordinates. macOS treats multiple monitors as one large coordinate space. Ensure your window detection logic accounts for negative X coordinates (which occur if a window is on a monitor to the left of the primary display).
Is there a way to do this without granting Screen Recording permissions?
No. macOS security architecture requires explicit user consent for any application to access the frame buffer. There is no programmatic way to bypass this for security reasons.
Can I capture just the webpage content without the browser tabs and address bar?
To do this, you must first find the window coordinates and then add a vertical offset (usually around 80-120 pixels) to the Y coordinate to skip the toolbar area. The exact offset depends on your Chrome theme and window size.