Python: how to open default browser using webbrowser module?

Viewed 10642

So I'm fairly new to the language, and I wanted to play music from a site. Using the webbrowser module, I executed the code below I was told that it will open the site in the default browser.

import webbrowser
webbrowser.open("Youtube.com")

It works just as expected but opens the site using the almighty Internet Explorer which, we all know is no one's default browser. If it helps in anyway, my default browser is Google Chrome

3 Answers

Try using get():

webbrowser.get('chrome').open('https://www.youtube.com')

EDIT

Make sure to use the full path to the site

Try this:

>>> import webbrowser
>>> browser_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
>>> url = "https://www.youtube.com"
>>> webbrowser.get(browser_path).open(url)
True

Note the Unix-style path. This is because webbrowser internally does a shlex.split on the path, which will just erase Windows-style path separators

Registering the browser also works on Windows:

>>> import webbrowser as wb
>>> wb.register('chrome', None)
>>> wb.open('https://www.youtube.com')
True
>>> wb.open('https://www.google.com')
True
>>> wb.open('https://stackoverflow.com')
True

use

import webbrowser as wb
wb.get('windows-default').open('Youtube.com'

For a particular browser, you can try using:

wb.get('chrome %s').open('Youtube.com') # for chrome
Related