我曾尝试使用webbrowser.open进行python,但它只适用于IE。如何让它打开chrome或firefox。我不希望它在IE上打开,我想在Chrome或Firefox上打开。由于我尝试了很多方法,但它们都不起作用。
import time
import webbrowser
webbrowser.open('www.google.com')
答案 0 :(得分:1)
您需要指定webbrowser's name
,另请参阅webbrowser.get
import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True
<强> 更新 强>
如果您的计算机中安装了chrome
或firefox
,请执行以下操作:
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
否则你可以尝试selenium,它提供更多功能,只需要chromedriver。