1

This is a Win10 machine.

I have a file on disk called "test.html". When I run this in a terminal window C:\>test.html my default browser opens, and that is chrome.

Now I do the same in python

C:\> copy con test.py
import webbrowser
webbrowser.open('http://www.python.org')
^Z
    1 file(s) copied.
C:\>python test.py

and Chrome, the default browser opens

But when I take an HTML response from an API call, and do the same webbrowser.open(...) then Internet Explorer opens. Not Edge, not Chrome, but Internet explorer.

How? It's almost as if there is something in the response that tells it "open with IE" except that if do it manually from the command line with C:\>response.html it opens chrome too.

Where is this instruction to open Internet Explorer coming from?

4
  • What do you mean "take an HTML response from an API call" ? Are you passing something that's not a URL to webborwser.open() ? Commented Jan 10, 2022 at 22:57
  • Yes, I was taking the HTML document, and passing that. Commented Jan 11, 2022 at 1:31
  • 1
    webbrowser.open() is only documented as supporting opening a URL, and best effort of opening a file, it does not support opening a HTML string directly, I'd expect nearly any behavior when using it incorrectly. Commented Jan 11, 2022 at 1:55
  • Rightyo, thanks for pointing that out Commented Jan 11, 2022 at 10:01

2 Answers 2

2

You can try webbrowser.get().

For example, opening a new tab in Google Chrome:

webbrowser.get(using='google-chrome').open_new_tab('https://google.com')

But it is not always possible to get by with .get () alone, and in this case the .register () function comes to the rescue, for example:

import webbrowser
webbrowser.register('Chrome', None, webbrowser.BackgroundBrowser('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'))
webbrowser.get('Chrome').open_new_tab('google.com')

You can also check these links:

https://docs.python.org/3/library/webbrowser.html http://pymotw.com/2/webbrowser/ https://discourse.world/h/2019/10/10/How-to-open-a-link-in-Python.Working-with-WebBrowser-and-solving-a-problem-with-Internet-Explorer

Sign up to request clarification or add additional context in comments.

1 Comment

The question was about opening a static html file. get("http://...") seems to work fine
2

It should use your default browser. Otherwise, it falls back to IE

It appears to use os.startfile() to detect file associations, so running os.startfile('response.html') apparently throws a OSError for you, and you have no other browser installed in the list. Surprisingly, "chrome" nor "edge" is listed there.

Related - https://github.com/python/cpython/pull/11327

Another option you could try is to use something like webbrowser.open("file://c/response.html")

3 Comments

Its interesting that "chrome" isnt listed in the browsers to fall back on in win, though I do see others like "firefox" listed apparently.
Falls back to IE... Yes, it certainly looks like that. But I do have three other browsers installed, and Chrome is my default. I'm guessing there is a an environmental con figuration bug somewhere in my OS
@Maxcot Well, you can see the source code which browsers it tries to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.