3

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

示例代码:

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

提前致谢!

4

3 回答 3

2

好吧,找到问题了。我的浏览器正确默认为 chrome,问题是 webbrowser.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”添加到(列表)中的浏览器列表中。

于 2014-06-04T17:52:52.300 回答
1

按照文档,您可以按照以下几个方向进行操作:

  1. 设置环境变量BROWSER
  2. 用于webbrowser.get('chrome')获取 Chrome 的控制器实例,然后使用它进行浏览
  3. 检查您的设置——您确定您的默认浏览器设置正确吗?它是否出现在“开始”菜单中的“Internet”图标下?
于 2014-06-04T16:14:16.120 回答
0

在 Windows 中,以下代码适用于我。

chrome_path = '"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" %s'
webbrowser.get(chrome_path).open('google.com')
于 2021-05-05T10:03:17.013 回答