1

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

MLavrentyev
  • 1,827
  • 2
  • 24
  • 32
  • 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 try – DeepSpace Jun 10 '19 at 00:59
  • 1
    @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 Jun 10 '19 at 01:02
  • try registering it? `webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path)) ` – R4444 Jun 10 '19 at 01:03
  • Yes, it is pre-installed in python. I tried your code and I got the same output. – Arnav Poddar Jun 10 '19 at 01:05
  • 1
    @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 Jun 10 '19 at 01:09
  • 1
    In Chrome, automatic navigation to `chrome://` paths is prohibited. Likely to prevent malicious interaction with browser settings. – JamesJJ Jun 10 '19 at 01:14
  • 1
    @JamesJJ Any source to back up that claim? It certainly works on my MacBook. – Selcuk Jun 10 '19 at 01:16
  • @Selcuk is executing `chrome_path chrome://version` in the terminal working for you? – DeepSpace Jun 10 '19 at 01:20
  • @DeepSpace That doesn't work (a regular URL works though). – Selcuk Jun 10 '19 at 01:25
  • Try: `webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path), 1) ` – R4444 Jun 10 '19 at 01:31
  • @Ruturaj still the same result as in my above comment. – MLavrentyev Jun 10 '19 at 01:57

1 Answers1

1

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.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    This is a good answer as to a possibility, though I'm still wondering why this is (and why not the case on Mac per @Selcuk's above comments) and how to work around this – MLavrentyev Jun 10 '19 at 01:20
  • I disagree with the first half of this (Up to the last paragraph). When I tested it, I was able find that the `:` in the `url` variable of `chrome://dino` is where webbrowser stops reading the url. Therefore, it only opens up `chrome`. – Arnav Poddar Jun 10 '19 at 01:20
  • 1
    You can see that `import webbrowser chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s" url = "chrome//dino" webbrowser.get(chrome_path).open(url)` works perfectly fine, without the `:` – Arnav Poddar Jun 10 '19 at 01:21
  • @MLavrentyev As to the *why*, I don't know, ask Google. As to *how to get around this*. use a tool that allows this, ie selenium – DeepSpace Jun 10 '19 at 01:21
  • @ArnavPoddar Does not work for me. Chrome opens up and tries to access `https://chrome//dino` – DeepSpace Jun 10 '19 at 01:22
  • 1
    @ArnavPoddar the `:` seems to only be a problem for internal sites specifically, since opening `about:about` in the terminal for Firefox works. Also, opening `https://stackoverflow.com` also works. – MLavrentyev Jun 10 '19 at 01:27
  • Oh, that's interesting. @MLavrentyev I thought the string wasn't fully reading, but it is, in fact, another issue that is probably what was stated in the above answer. – Arnav Poddar Jun 10 '19 at 19:53
  • I'm accepting this, since this is the best answer and I don't see any more forthcoming. – MLavrentyev Jun 25 '19 at 01:44