3

I am trying to open a page a wrote and saved to a local server. Everything is great but it defaults to opening in IE instead of Chrome. Chrome is my default browser and couldn't find any helpful tips online.

Sample code:

import webbrowser
webbrowser.open('192.168.1.254:1337/SmartFormTest1.php')

Thanks in advance!

user3681278
  • 135
  • 1
  • 4
  • 11

4 Answers4

2

Alright, found the issue. My browser was correctly defaulted to chrome, the issue is the webbrowser.py file. Lines 539-563 read:

if sys.platform[:3] == "win":
class WindowsDefault(BaseBrowser):
    def open(self, url, new=0, autoraise=True):
        try:
            os.startfile(url)
        except WindowsError:
            # [Error 22] No application is associated with the specified
            # file for this operation: '<URL>'
            return False
        else:
            return True

_tryorder = []
_browsers = {}

# First try to use the default Windows browser
register("windows-default", WindowsDefault)

# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                        "Internet Explorer\\IEXPLORE.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
                "netscape", "opera", iexplore):
    if _iscommand(browser):
        register(browser, None, BackgroundBrowser(browse()

All I needed to do was add "chrome" to the list of for browser in (list).

user3681278
  • 135
  • 1
  • 4
  • 11
1

Following the documentation, there are a few directions you can go with this:

  1. Set the environment variable BROWSER
  2. Use webbrowser.get('chrome') to get a controller instance of Chrome, then use that to do your browsing
  3. Check your setup -- are you positive that your default browser is set properly? Does it appear under the "Internet" icon in your Start menu?
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • 'webbrowser.get('chrome')' Or any other name, ('mozilla') for example, returns the following error: File "C:\Python27\lib\webbrowser.py", line 52, in get raise Error("could not locate runnable browser") webbrowser.Error: could not locate runnable browser I also checked that Chrome was the default browser on my machine as well as on the server. – user3681278 Jun 04 '14 at 16:42
  • @user3681278 Do you have the same problem if you use `foo = webbrowser.Chrome(); foo.open(some_url)`? – Patrick Collins Jun 04 '14 at 17:15
  • Also, although it doesn't sound like it'll fix your problem, make sure you have `http://` at the beginning of your url. – Patrick Collins Jun 04 '14 at 17:17
  • Found the issue, see my posted answer: http://stackoverflow.com/a/24044266/3681278 – user3681278 Jun 04 '14 at 17:54
0

In Windows, the following code works for me.

chrome_path = '"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" %s'
webbrowser.get(chrome_path).open('google.com')
4b0
  • 21,981
  • 30
  • 95
  • 142
0

My browser was correctly defaulted to brave, just change it in the webbrowser.py file. Lines 539-563

In Line 540, just change the OS path to the desired browser you want to use. For Brave just change the path given to the iexplorer variable like this:

iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                                "BraveSoftware\\Brave-Browser\\Application\\brave.EXE")
Physicing
  • 532
  • 5
  • 17
SID
  • 1