2016-06-20 43 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') 
相关问题