windows 获取chrome 浏览器位置


import winreg

_browser_regs = {
    'IE': r"SOFTWARE\Clients\StartMenuInternet\IEXPLORE.EXE\DefaultIcon",
    'chrome': r"SOFTWARE\Clients\StartMenuInternet\Google Chrome\DefaultIcon",
    'edge': r"SOFTWARE\Clients\StartMenuInternet\Microsoft Edge\DefaultIcon",
    'firefox': r"SOFTWARE\Clients\StartMenuInternet\FIREFOX.EXE\DefaultIcon",
    '360': r"SOFTWARE\Clients\StartMenuInternet\360Chrome\DefaultIcon",
}


def get_browser_path(browser):
    """
    获取浏览器的安装路径

    :param browser: 浏览器名称
    """
    try:
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _browser_regs[browser])
    except FileNotFoundError:
        raise Exception("没找到可以执行的chrome程序.")
    value, _type = winreg.QueryValueEx(key, "")
    if value.split(',')[0].strip() == "":
        raise Exception("没找到可以执行的chrome程序.")
    return value.split(',')[0]


browser='chrome'
aa=get_browser_path(browser)
print(aa)


  目录