2016-06-20 68 views
4

我已經看到一些與此相關的問題,但我仍然遇到問題。當Firefox不是默認瀏覽器時,使用Firefox打開HTML文件

運行代碼:與

>>>webbrowser.get('firefox')

錯誤:

webbrowser.Error: could not locate runnable browser

要解決我跑:

>>>print(webbrowser._browser)

{'windows-default': [<class 'webbrowser.WindowsDefault'>, None], 'c:\\program files\\internet explorer\\iexplorer.exe': [None, <webbrowser.BackgroundBrowser object at 0x000000000651FEB8>]}

奇怪的是,我安裝了Firefox,它是我的默認瀏覽器,並且我試圖通過Python打開的HTML文件以Firefox打開。

除了我需要將此程序發送給可能將IE設置爲其默認Windows默認設置的用戶,並且必須在Firefox中打開HTML文件之外,所有內容都適用於該世界。

+1

你可以安裝硒和做webdriver.Firefox() –

回答

1

除了我需要將該程序發送給可能將IE設置爲其默認Windows默認設置的用戶,並且必須在Firefox中打開HTML文件之外,所有這一切都適合世界。

一個解決這個問題的方法是使用selenium瀏覽器自動化包。您可以與open local HTML files還有:

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get("file:///D:/folder/abcd.html") 
+0

我會試試看。更復雜的問題是,所有的用戶都在公司網絡上安裝Python 3.5。使用標準庫時你有什麼建議嗎? –

+0

@RussellB。 「硒」可能是最可靠的選擇。但是,您可以使用'webbrowser'在自定義路徑中打開瀏覽器 - Chrome的示例:http://stackoverflow.com/questions/22445217/python-webbrowser-open-to-open-chrome-browser。另請參閱http://stackoverflow.com/questions/5916270/pythons-webbrowser-launches-ie-instead-of-default-on-windows-7。 – alecxe

+0

@RussellB。 'virtualenv' – JakeD

1

添加%s通過火狐打開它的路徑的末尾。

webbrowser.get('C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s') 
相關問題