Search code examples
pythonpython-webbrowser

Reaching internal chrome pages with Python webbrowser


I have a program where I'm using Python's webbrowser module to open a browser and navigate to a page automatically. My code essentially looks like the following:

import webbrowser

chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
url = "stackoverflow.com"

webbrowser.get(chrome_path).open(url)

When doing it with a normal site it works exactly as expected. However, when I instead substitute in an internal Chrome site of the format chrome://<page> (e.g. chrome://dino or chrome://version) for the url, Chrome opens as expected, but it does not navigate anywhere but rather instead stays on my new tab page.

Why are normal urls (and even strings such as "hello world") working as expected, but only chrome-specific pages not? Is there any way to get around this?

(This is on Windows 10 & Python 3.6.8 by the way).


Solution

  • It indeed does not work, but it's not webbrowser's fault.

    A small diving into the code shows that, at the end of the day, webbrowser simply calls subprocess.Popen(args) where args ending up being

    'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe <url>'.

    If you simply open a terminal window and execute

    "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" chrome://dino

    you will get the exact same behavior: Chrome opens and stays on the home page, so the problem is lying somewhere in Chrome's code (either a bug or a design choice).

    It works with selenium since I assume it is using some black-OS-magic (ie interprocess communication) so it does not rely on Chrome's code. It just mimics a user.