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

3

6

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.

Ebarta answered 7/2, 2020 at 0:28 Comment(4)
You should supply a piece of code that people can use to investigate.Dodona
The current code as-is will work, all that's needed is a way to trigger the event variable, which could just be event = input() or similar, plus a file called atextfilewithalinkinit.txt that has an URL in it.Leotaleotard
If you want people to help you with this rather obscure platform dependent issue, you should provide them with a minimum working example such that they can run the code and help you debug.Dodona
I've made very minor changes to the code so if you paste the three bits together, it now runs as-is. Of course I can't also provide you a text file, but there's only so much hand-holding I can do! The text file just needs to have https://www.google.com in it or similar.Leotaleotard
D
1

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/

Dodona answered 16/2, 2020 at 3:6 Comment(1)
Thank you for this, it didn't actually fix the problem at all, but it allowed me to troubleshoot. I rewrote the code to use the subprocess module and now it makes both Chrome AND Firefox crash in Windows 7 (but again, works fine in Win10). So this makes me think that it's just not worth even trying to add this feature because the problem is bigger than the code. I'll just let it do the default browser for now and at a later date when every machine I have to install this software on is Win10 I'll roll it out at that time.Leotaleotard
G
0

Have you tried the webbrowser module?

import webbrowser
webbrowser.get('firefox %s').open('http://google.com')

You can see a list of browsers and what they are called here webbrowser docs

Glean answered 14/2, 2020 at 15:56 Comment(2)
You haven't read the question properly. The program already uses the webbrowser module.and webbrowser.get. The code works fine - except in certain conditions. Read the question, including the bolded text, again.Leotaleotard
I can't recreate this problem.Glean
G
0

if i get it right you wanna open a tab in your desire browser rather than default browser in this case you can use the get method in webbrowser library

the simple code that works for me was :

import webbrowser as wb

wb.get('google-chrome') #the return answer was : <webbrowser.Chrome object at 0x7fe787065a50>

wb.get('google-chrome').open('https://url')

Here is the list for the supported browsers:

enter image description here

Goatee answered 14/2, 2020 at 17:15 Comment(1)
You haven't read the question properly. The program already uses the webbrowser module.and webbrowser.get. The code works fine - except in certain conditions. Read the question, including the bolded text, again.Leotaleotard

© 2022 - 2024 — McMap. All rights reserved.