1

I want a way to find the default browser in Python. I've seen https://docs.python.org/3.8/library/webbrowser.html and there might be a way of converting a webbrowser instance which I do not know.

Does anybody know how I could go about doing that?

Also, I'm using Windows - it doesn't need to work for Mac or Ubuntu.

Edit:

I've already tried this website and it gives me an error saying that 'the file path does not exist'.

Additionally, I don't want to open a tab in the default browser, I just want the name.

Edit #2:

The following code works but returns Internet Explorer instead of my actual default which is Chrome.

from winreg import*
with OpenKey(HKEY_CLASSES_ROOT,r"http\\shell\\open\\command") as key:
    cmd = QueryValue(key, None)
Professor Dragon
  • 217
  • 1
  • 4
  • 14
  • Does this fit you? https://stackoverflow.com/questions/19037216/how-to-get-a-name-of-default-browser-using-python – Chris Mar 28 '20 at 03:03
  • @Chris That returns an error saying the file path does not exist. – Professor Dragon Mar 28 '20 at 03:05
  • Here is a more indepth article: https://newoldthing.wordpress.com/2007/03/23/how-does-your-browsers-know-that-its-not-the-default-browser/ – Chris Mar 28 '20 at 03:10
  • Does this answer your question? [How to get name of the default browser in windows using python?](https://stackoverflow.com/questions/32681951/how-to-get-name-of-the-default-browser-in-windows-using-python) – Joshua Varghese Mar 28 '20 at 03:12

2 Answers2

1

I have figured it out. I'm pretty sure this only works on Windows, but here's the code:

from winreg import *
with OpenKey(HKEY_CURRENT_USER, r"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice") as key:
    browser = QueryValueEx(key, 'Progid')[0]

It will return ChromeHTML for Chrome, FirefoxURL for Firefox, and IE.HTTP for Internet Explorer.

Professor Dragon
  • 217
  • 1
  • 4
  • 14
0

You can try this

import webbrowser

default_browser = webbrowser.get()

default_browser_name = default_browser.name
default_browser_basename = default_browser.basename

As the docs say

webbrowser.get([name])

Return a controller object for the browser type name. If name is empty, return a controller for a default browser appropriate to the caller’s environment.

Henry Harutyunyan
  • 2,355
  • 1
  • 16
  • 22