How to get default browser name using python?

。_饼干妹妹 提交于 2020-08-08 05:26:49

问题


Following solutions (actually it is only one) doesn't work to me :

How to get a name of default browser using python


How to get name of the default browser in windows using python?

Solution was:

from _winreg import HKEY_CURRENT_USER, OpenKey, QueryValue
# In Py3, this module is called winreg without the underscore

with OpenKey(HKEY_CURRENT_USER,
             r"Software\Classes\http\shell\open\command") as key:
    cmd = QueryValue(key, None)

But unfortunately, in Windows 10 Pro I don't have targeted registry value. I've tried to find alternative keys in Regedit, but no luck.

Please take a look, what my registry virtually contains:


回答1:


The following works for me on Windows 10 pro:

from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx

reg_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'

with OpenKey(HKEY_CURRENT_USER, reg_path) as key:
    print(QueryValueEx(key, 'ProgId'))

Result (first with Chrome set as default, then with IE):

$ python test.py
('ChromeHTML', 1)

$ python test.py
('IE.HTTPS', 1)



回答2:


Please check for the key in windows 10

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations(http|https)\UserChoice




回答3:


def get_windows_default_browser_launch():
    """ On windows, return the default browser for 'https' urls
    returns: example '"C:\Program Files\Mozilla Firefox\firefox.exe" -osint -url "%1"'
    """
    import winreg
    key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), r"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice")
    prog_id, _ = winreg.QueryValueEx(key, "ProgId")
    key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE), r"SOFTWARE\Classes\{}\shell\open\command".format(prog_id))
    launch_string, _ = winreg.QueryValueEx(key, "")  # read the default value
    return launch_string

Windows 10 Python3 , may want to change the key for 'http' not https, but this is my code verbatim as my context is of a secured server. I wanted the browser binary name and path, which is just one more line.



来源:https://stackoverflow.com/questions/53926506/how-to-get-default-browser-name-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!