Python2.7更改默认浏览器网络浏览器打开

2024-05-13 15:24:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图打开一个已写入并保存到本地服务器的页面。一切都很好,但它默认在IE中打开而不是Chrome。Chrome是我的默认浏览器,在网上找不到任何有用的提示。在

样本代码:

import webbrowser
webbrowser.open('192.168.1.254:1337/SmartFormTest1.php')

提前谢谢!在


Tags: 代码import服务器浏览器页面openchromeie
2条回答

好吧,找到问题了。我的浏览器被正确地默认为chrome,问题是网络浏览器.py文件。第539-563行内容如下:

if sys.platform[:3] == "win":
class WindowsDefault(BaseBrowser):
    def open(self, url, new=0, autoraise=True):
        try:
            os.startfile(url)
        except WindowsError:
            # [Error 22] No application is associated with the specified
            # file for this operation: '<URL>'
            return False
        else:
            return True

_tryorder = []
_browsers = {}

# First try to use the default Windows browser
register("windows-default", WindowsDefault)

# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                        "Internet Explorer\\IEXPLORE.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
                "netscape", "opera", iexplore):
    if _iscommand(browser):
        register(browser, None, BackgroundBrowser(browse()

我需要做的就是把“chrome”添加到for browser in(列表)的列表中。在

documentation之后,您可以使用以下几种方法:

  1. 设置环境变量BROWSER
  2. 使用webbrowser.get('chrome')获取Chrome的控制器实例,然后使用该实例进行浏览
  3. 检查你的设置你确定你的默认浏览器设置正确吗?它是否出现在“开始”菜单中的“Internet”图标下?在

相关问题 更多 >