1
votes

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).

1
I can't speak for webbrowser, but I can confirm that Chrome('chromedriver.exe').get('chrome://dino') works in selenium. You might want to give that a tryDeepSpace
@DeepSpace I'll give selenium a try but I am still hoping to stick with webbrowser since it's a bit simpler and (I believe) already comes with Python.MLavrentyev
@Ruturaj when I run your line followed by webbrowser.get('chrome').open("stackoverflow.com") I now instead get it returning False and not opening Chrome at all.MLavrentyev
In Chrome, automatic navigation to chrome:// paths is prohibited. Likely to prevent malicious interaction with browser settings.JamesJJ
@JamesJJ Any source to back up that claim? It certainly works on my MacBook.Selcuk

1 Answers

1
votes

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.