User Agents

Web browsers and everyday web tech, explained

Automate Chromium Extension Interactions with Python

· 5 min read · Extensions

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

This guide shows you how to automate clicks, form fills, and other interactions with a Chromium browser extension using Python. You'll use Selenium to drive a real Chromium instance and access the extension's popup or content scripts, enabling end-to-end testing or repetitive workflow automation. You'll need Python installed, a Chromium-based browser (like Chrome or Edge), and the Selenium package.

What You'll Need

  • Python 3.7+ installed on your system
  • A Chromium-based browser (Google Chrome, Microsoft Edge, or Chromium itself)
  • The Selenium package for Python (install via pip install selenium)
  • A WebDriver for your browser (ChromeDriver for Chrome, EdgeDriver for Edge)
  • Your extension's .crx file or its unpacked source folder
  • Basic understanding of HTML and JavaScript (to identify elements to interact with)

Step-by-Step

  1. Install Selenium and WebDriver
    Run pip install selenium. Download the matching WebDriver for your browser version (e.g., ChromeDriver from chromedriver.chromium.org). Place the WebDriver executable in your PATH or note its location.
  2. Prepare your extension
    If you have an unpacked extension folder, keep it accessible. If you have a .crx file, you can load it directly. Note the extension's ID (you can find it in chrome://extensions after loading).
  3. Set up Selenium with extension loading
    In your Python script, create Chrome options and add the extension. For unpacked folders, use options.add_argument('--load-extension=/path/to/folder'). For .crx files, use options.add_extension('/path/to/extension.crx'). Then instantiate the driver with these options.
  4. Launch the browser and wait for the extension to load
    Start the driver, then wait a few seconds for the extension to initialize. Use time.sleep(5) or better, Selenium's WebDriverWait to wait for a known element in the extension's popup.
  5. Access the extension's popup or content script
    Extensions often have a popup that opens when clicking the toolbar icon. You can navigate to the extension's popup URL directly: chrome-extension://EXTENSION_ID/popup.html. Replace EXTENSION_ID with your actual ID. Alternatively, if the extension runs content scripts on web pages, navigate to a target page and interact with the injected elements.
  6. Interact with elements
    Use Selenium's standard methods (find_element, click, send_keys) to interact with buttons, inputs, and other elements inside the extension's popup or content script. For example, to click a button with id "save", use driver.find_element(By.ID, 'save').click().
  7. Handle iframes or shadow DOM if present
    Some extensions use iframes or shadow DOM. Switch to an iframe using driver.switch_to.frame(). For shadow DOM, use JavaScript execution to pierce the shadow root: driver.execute_script('return document.querySelector("my-element").shadowRoot').
  8. Close the browser and clean up
    After automation, call driver.quit() to close the browser and release resources.

Common Mistakes

  • Incorrect extension ID: Copy the ID exactly from chrome://extensions after loading the extension. It's case-sensitive.
  • Not waiting for extension load: Extensions load asynchronously. If you try to interact immediately, the elements may not exist. Always wait.
  • WebDriver version mismatch: Ensure your WebDriver matches your browser's exact version. A mismatch causes session errors.
  • Using wrong URL for popup: The popup URL may not be popup.html; check the extension's manifest file for the default popup path.
  • Forgetting to switch to iframes: If the extension content is inside an iframe, Selenium won't find elements until you switch.

Tips

  • Use WebDriverWait with expected conditions instead of fixed sleeps for more reliable automation.
  • Run the browser in headless mode (options.add_argument('--headless')) for faster execution, but note some extensions may not work headless.
  • Keep your extension's source folder in a stable location; moving it after loading may break the automation.
  • If you need to interact with the extension's background service worker, use driver.execute_script to call chrome.runtime.getBackgroundPage() or use the debugger protocol.
  • For complex interactions, consider using the Chrome DevTools Protocol (CDP) via Selenium's driver.execute_cdp_cmd() for advanced control.

Troubleshooting

Extension not loading: Check that the path to the folder or .crx file is correct. For unpacked extensions, ensure the manifest.json is present. Try loading the extension manually in the browser to confirm it works.

Cannot find elements: Open the popup URL manually in the browser and inspect the DOM. Use browser devtools to verify element IDs and classes. If the content is in a shadow DOM, you'll need to pierce it via JavaScript.

WebDriver session crashes: Update your WebDriver to match the browser version. Also, ensure no other instance of the browser is using the same user profile; use a fresh profile by adding --user-data-dir=/tmp/custom_profile.

Extension popup closes immediately: Some popups close when they lose focus. Use driver.switch_to.window(driver.window_handles[0]) to keep focus, or interact via content scripts instead.

Headless mode issues: Some extensions require a visible browser. Remove --headless and run with a virtual display if needed (e.g., Xvfb on Linux).

Frequently Asked Questions

Can I automate a Chrome Web Store extension without the source code?

Yes, if you have the .crx file, you can load it directly with options.add_extension(). You can obtain the .crx from the Chrome Web Store or from your browser's cache.

How do I find the extension ID?

After loading the extension, go to chrome://extensions, enable Developer mode, and you'll see the ID in the extension's card. Alternatively, check the URL when you click "Details" – it contains the ID.

Can I interact with the extension's background script?

Yes, you can use driver.execute_script() to call chrome.runtime.getBackgroundPage() and then access its variables and functions. This works if the extension has a background page (not a service worker). For service workers, use CDP.

Why does my extension popup not open when I click the toolbar icon in Selenium?

Selenium doesn't simulate the browser UI. Instead of clicking the icon, navigate directly to the popup URL. That's the recommended approach.

Is it possible to automate extensions that use shadow DOM?

Yes, you can pierce shadow DOM using JavaScript. For example: driver.execute_script('return document.querySelector("my-element").shadowRoot') and then find elements within that root.

Can I use this method for Firefox extensions?

No, this guide is specific to Chromium-based browsers. Firefox uses a different WebDriver and extension model, but the general concept might be adapted with geckodriver.