Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python webbrowser.open() to open Chrome browser

According to the documentation http://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?

like image 699
user1527216 Avatar asked Oct 09 '22 06:10

user1527216


People also ask

How do I open a specific browser in python?

To open a page in a specific browser, use the webbrowser. get() function to specify a particular browser.

How do I open a browser URL from a python script?

just open the python interpreter and type webbrowser. open('http://www.google.com') and see if it does what you want. yes. The result is same.


2 Answers

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 = 'http://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)
like image 124
Chad Jones Avatar answered Oct 10 '22 20:10

Chad Jones


In the case of Windows, the path uses a UNIX-style path, so make the backslash into forward slashes.

webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("http://google.com")

See: Python: generic webbrowser.get().open() for chrome.exe does not work

like image 36
Zacarias Bendeck Avatar answered Oct 10 '22 18:10

Zacarias Bendeck