python打开其他浏览器异常webbrowser.Error: could not locate runnable browser

陆承宣
2023-12-01

python打开其他浏览器异常webbrowser.Error: could not locate runnable browser

问题描述

  • 运行python代码
import webbrowser

print(webbrowser.get('firefox').open('http://www.python.org'))

异常信息

Traceback (most recent call last):
  File "f:/workspace/python workspace/python cookbook/prj13/prj13_15/src/test.py", line 3, in <module>
    c = webbrowser.get('firefox')
  File "F:\sdk\python\Python37\lib\webbrowser.py", line 65, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

分析原因

  • 可能是找不到浏览器,浏览器需要注册一下

解决方法

import webbrowser

url = 'https://docs.python.org/3/library/webbrowser.html'
firefox_path = "D:\\Program Files\\internet software\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(firefox_path))
print(webbrowser.get('firefox').open_new_tab(url))
 类似资料: