QA Graphic

Getting the Current URL Using Python in MacOS

Example using Chrome or Safari

Welcome to my latest blog post! As a QA professional with five years of experience in automation programming using Python, I've often encountered scenarios where I needed to fetch the current URL from a browser - be it for testing, automation, or data extraction. Today, I'm going to walk you through a simple yet effective way to get the current URL from Chrome or Safari on MacOS using Python.

Why Fetch URLs Programmatically?

Fetching URLs programmatically can be useful in various scenarios, such as:

  • Automating tests that require validating the current page in a browser.
  • Monitoring web usage or collecting data for analysis.
  • Building extensions or integrations that react to URL changes.

Python Code Chrome

Prerequisites

Before we dive in, ensure you have Python installed on your MacOS. You can check this by running python -version in your terminal. Additionally, install the required libraries using pip:

pip install pyobjc-framework-ScriptingBridge

or

python3 -m pip install pyobjc-framework-ScriptingBridge --user

This library allows Python to interact with MacOS applications via the ScriptingBridge framework.

Fetching URL from Chrome

Let's start with Google Chrome. Chrome, like many modern browsers, exposes its current state through AppleScript, which we can access in Python using the ScriptingBridge framework.

The Python Script


import ScriptingBridge
def get_chrome_url():
    chrome = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_("com.google.Chrome")
    if not chrome.windows():
        return "No active window"
    
    window = chrome.windows()[0]  # Get the first window
    tab = window.activeTab()  # Get the active tab in the window
    return tab.URL()  # Return the URL of the active tab
print(get_chrome_url())

This script initializes a connection to Chrome, checks if there are any open windows, and then fetches the URL of the active tab in the first window.

Chrome Example

Fetching URL from Safari

The process for Safari is quite similar. However, Safari's bundle identifier differs.

The Python Script for Safari


import ScriptingBridge
def get_safari_url():
    safari = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_("com.apple.Safari")
    if not safari.windows():
        return "No active window"
    
    window = safari.windows()[0]
    return window.currentTab().URL()
print(get_safari_url())

Here, we connect to Safari and fetch the URL from the current tab of the first window.

Safari Python Example

Handling Multiple Windows or Tabs

If you need to handle multiple windows or tabs, you can loop through the windows array and fetch URLs from each tab. This can be particularly useful for comprehensive testing or data extraction tasks.

Security and Permissions

Since MacOS Mojave, apps need explicit permissions to control other apps. The first time you run these scripts, MacOS should prompt you to grant Terminal (or your Python IDE) access to control the browser. Make sure to allow this for the script to function correctly.

Conclusion

Fetching the current URL from browsers like Chrome and Safari is straightforward with Python and ScriptingBridge in MacOS. This technique opens up a range of possibilities for automation, testing, and data collection. Experiment with it, and you'll find it a valuable addition to your Python automation toolkit.