Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the default browser selection in Windows 7 when opening webppages with Python

I have a program written with Python 3.7.4 on Windows that opens web pages in a browser. It displays the default browser and gives the user the chance to change which browser they want to use to open programs with.

The default browser is detected when the program is initialised using this technique:

from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx

import webbroswer

reg_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
firefox_path = 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s'

with OpenKey(HKEY_CURRENT_USER, reg_path) as key:
    print(QueryValueEx(key, 'ProgId'))
    thedefaultbrowser = (QueryValueEx(key, 'ProgId'))
    thedefaultbrowser = (thedefaultbrowser[0])
    thedefaultbrowser = (thedefaultbrowser[0:2])
    if thedefaultbrowser == "Fi":
        browser_path = firefox_path
    if thedefaultbrowser == "Ch":
        browser_path = chrome_path

And links are opened like this, as part of an infinite loop that happens later in the program:

while True:

    # [lots of GUI code that isn't relevant]

    event = input()

    if event == 'pleasegetmealinknao':
        x=open("C:/folder/atextfilewithalinkinit.txt", "r")
        the_url1 = x.read()
        webbrowser.get(browser_path).open(the_url1)
        x.close()     

In the same loop, input can be accepted from the user to change the browser type:

    elif event == "Firefox":
        browser_path = firefox_path
        continue
    elif event == "Chrome":
        browser_path = chrome_path
        continue       

So I can open a webpage with this just fine. I can then change the browser type just fine, and then open another webpage in the newly user-selected browser, but - only in Windows 10.

In Windows 7, Firefox co-operates beautifully, and so does Chrome but only if Chrome is set as the default browser before the program runs. However if Firefox is set as the default browser before the program is started, as soon as I switch to Chrome and then try to open anything in Chrome, the program gets very upset and stops running. No crash, no error message or traceback, it just freezes completely and needs to be hard-restarted. Sometimes I can rescue the program from its frozen state by closing the new browser window that was just opened, but this of course defeats the purpose of the program.

HOWEVER - if I scrap all this fancy business and just accept whatever the default browser is, like this:

    if event == 'pleasegetmealinknao':
        x=open("C:/folder/atextfilewithalinkinit.txt", "r")
        the_url1 = x.read()
        webbrowser.open(the_url1)
        x.close()  

Then the chosen link will open happily in whatever the system default browser is, whether it be Firefox, Chrome, Internet Explorer, or anything else.

So why is this happening and how can I fix? Or do I just have to bury my dream of selectable browsers?

My guess is that this might have something to do with Chrome detecting that it is not the default browser and asking the user to change the default browser setting, that would make sense. However then the question is, how do I make the Python program ignore whatever this behaviour is. A popup related to this doesn't always appear.

EDIT: further testing and sometimes the Windows 7 pattern of behaviour also happens in Windows 10... but seemingly not all the time. This makes me think even more that it's browser-related rather than strictly OS related and has a lot to do with Chrome asking for default status, but I'm still no closer to working out what to do about it.

like image 233
츄 plus Avatar asked Feb 07 '20 00:02

츄 plus


1 Answers

I cannot test this issue as I do not have Windows 7. If the issue is that Chrome is checking whether it is the default browser, you can suppress that via the command line switch chrome.exe --no-default-browser-check $link. However, I do not think you can do this using the python webbrowser module. Instead, you would need to use subprocess or a terminal interacting module which is compatible with windows eg: pbs, and then call the webbrowser via the terminal. https://pypi.org/project/pbs/

like image 137
Sohrab T Avatar answered Oct 09 '22 06:10

Sohrab T