2017-05-23 83 views
0

我曾嘗試用webbrowser.open與python,但它只能在IE上運行。如何讓它打開鉻或Firefox。我不希望它在IE上打開,我想在Chrome或Firefox上打開。由於我嘗試了很多方法,但都沒有成功。有誰知道如何在窗口下使用python webbrowser.open

import time 
import webbrowser 
webbrowser.open('www.google.com') 

回答

1

你需要指定webbrowser's name,DETAL看到webbrowser.get

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

UPDATE
如果你有chromefirefox安裝在您的計算機,請執行下列操作:

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

+0

我嘗試用它\,它會彈出: >>> A = webbrowser.get( '火狐') 回溯(最近通話最後一個): 文件 「」,1號線,在 文件「C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ webbrowser.py」,第51行,獲取 raise錯誤(「找不到可運行瀏覽器」) webbrowser.Error:無法找到可運行的瀏覽器 >>> – jacklee26

+0

@ jacklee26查看我的更新 – Cheney

+0

非常感謝。它現在工作鉻。 – jacklee26

相關問題