0

I had try with python with webbrowser.open, but it only work on IE. How to let it open chrome or firefox. I don't want it to open on IE, i wants to be open on Chrome or Firefox. Due to i try many method, but none of them works.

import time
import webbrowser
webbrowser.open('www.google.com')
jacklee26
  • 47
  • 9
  • Look at this [How can I open a website in my web browser using Python?](https://stackoverflow.com/questions/31715119/how-can-i-open-a-website-in-my-web-browser-using-python/53963977#53963977) – Ali Moshiri Dec 28 '18 at 20:38

1 Answers1

1

you need specify your webbrowser's name, detal see webbrowser.get

import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True

UPDATE
If you have chrome or firefox installed in your computer, do as following:

chrome_path =r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' # change to your chrome.exe path  
# webbrowser is just call subprocess.Popen, so make sure this work in your cmd firstly
# C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com

# there two way solve your problem
# you have change \ to / in windows
# this seems a bug in browser = shlex.split(browser) in windows
# ['C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe', '%s']
a = webbrowser.get(r'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe %s')
a.open('www.google.com')  #True
# or by register 
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'))
a = webbrowser.get('chrome')
a.open('www.google.com')  #True

else you can try selenium, it provide much more functions and only need chromedriver.

Cheney
  • 960
  • 8
  • 23
  • i try with it \, and it will pop up: >>> a = webbrowser.get('firefox') Traceback (most recent call last): File "", line 1, in File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\webbrowser.py", line 51, in get raise Error("could not locate runnable browser") webbrowser.Error: could not locate runnable browser >>> – jacklee26 May 24 '17 at 00:43