vandelay
vandelay

Reputation: 2075

webbrowser, opening chrome and internet explore for different url

url = 'http://www.google.org/'
chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
webbrowser.get(chrome_path)
webbrowser.open(url)  

above will open chrome, which is what I want.

However if I change the url to url = 'reddit it will open internet explore instead. Why does it open different webbrowsers for different urls? And how can I make sure it opens in chrome for all urls?

Upvotes: 0

Views: 1080

Answers (1)

masnun
masnun

Reputation: 11916

Do this:

>>> import webbrowser
>>> browser = webbrowser.get()
>>> browser.open('http://google.com')
True
>>> browser.open_new_tab('http://yahoo.com')
True
>>>

The webbrowser.get() call will get you a browser controller object. You can run open, open_new and open_new_tab on the controller object. This will ensure the commands are executed on the same browser instance you opened.

If you directly use webbrowser.open() - it will always open the link in the default browser, which in your case is Internet Explorer.

So to rewrite your code:

chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
chrome = webbrowser.get(chrome_path)
chrome.open('http://google.com')
chrome.open_new_tab('http://reddit.com')

Upvotes: 1

Related Questions