Python open webpage in chrome

According to the documentation //docs.python.org/3.3/library/webbrowser.html it's supposed to open in the default browser, but for some reason on my machine it opens IE. I did a google search and I came across an answer that said I need to register browsers, but I'm not sure how to use webbrowser.register[] and the documentation doesn't seem to be very clear. How do I register Chrome so that urls I pass to webbrowser.open[] open in Chrome instead of IE?

asked Mar 17, 2014 at 0:50

1

You can call get[] with the path to Chrome. Below is an example - replace chrome_path with the correct path for your platform.

import webbrowser

url = '//docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files [x86]/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get[chrome_path].open[url]

Hassan Saeed

5,2671 gold badge30 silver badges34 bronze badges

answered Jun 22, 2014 at 17:28

Chad JonesChad Jones

1,2811 gold badge10 silver badges3 bronze badges

6

import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "//docs.python.org/library/webbrowser.html"
webbrowser.get[using='google-chrome'].open[url,new=new]

you can use any other browser by changing the parameter 'using' as given in a link

answered Apr 24, 2017 at 21:21

njn1234njn1234

3312 silver badges5 bronze badges

1

Please check this:

import webbrowser
chrome_path = 'C:/Program Files [x86]/Google/Chrome/Application/chrome.exe %s'
webbrowser.get[chrome_path].open['//docs.python.org/']

answered Jun 19, 2019 at 10:45

worked for me to open new tab on google-chrome:

import webbrowser

webbrowser.open_new_tab["//www.google.com"]

answered Sep 24, 2019 at 2:02

qloveshmilyqloveshmily

9318 silver badges5 bronze badges

Here's a somewhat robust way to get the path to Chrome.

[Note that you should do this only if you specifically need Chrome, and not the default browser, or Chromium, or something else.]

def try_find_chrome_path[]:
    result = None
    if _winreg:
        for subkey in ['ChromeHTML\\shell\\open\\command', 'Applications\\chrome.exe\\shell\\open\\command']:
            try: result = _winreg.QueryValue[_winreg.HKEY_CLASSES_ROOT, subkey]
            except WindowsError: pass
            if result is not None:
                result_split = shlex.split[result, False, True]
                result = result_split[0] if result_split else None
                if os.path.isfile[result]:
                    break
                result = None
    else:
        expected = "google-chrome" + [".exe" if os.name == 'nt' else ""]
        for parent in os.environ.get['PATH', ''].split[os.pathsep]:
            path = os.path.join[parent, expected]
            if os.path.isfile[path]:
                result = path
                break
    return result

answered May 26, 2019 at 8:10

user541686user541686

200k120 gold badges513 silver badges864 bronze badges

3

One thing I noticed and ran into problems with were the slashes, in Windows you need to have two of them in the path an then this works fine.

import webbrowser
chrome_path = "C://Program Files [x86]//Google//Chrome//Application//Chrome.exe %s"
webbrowser.get[chrome_path].open["//github.com/"]

This at least works for me

answered Jan 11, 2021 at 7:08

you can also use this:

import webbrowser

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
url = "//docs.python.org/"

webbrowser.register['chrome', None, webbrowser.BackgroundBrowser[chrome_path]]
webbrowser.get['chrome'].open_new_tab[url]

answered Mar 16, 2021 at 17:22

1

Worked for me in windows

Put the path of your chrome application and do not forget to put th %s at the end. I am still trying to open the browser with html code without saving the file... I will add the code when I'll find how.

import webbrowser
chromedir= "C:/Program Files [x86]/Google/Chrome/Application/chrome.exe %s"
webbrowser.get[chromedir].open["//pythonprogramming.altervista.org"]

>>> link to: [a page from my blog where I explain this]

Chủ Đề