You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python webbrowser库调用Chrome打开网页报错问题求助

解决Python webbrowser模块调用Chrome报错的问题

嘿,我之前也踩过这个坑!你遇到的Error: could not locate runnable browser错误,主要是两个小细节没处理对:

  • 路径的转义问题:在Python的字符串里,\是转义字符,你写的"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"里,\P、\G这些会被解析成特殊字符,导致实际路径和你想的不一样。
  • webbrowser.get()的参数格式:直接传浏览器exe的路径是不够的,这个方法需要你指定调用浏览器时的命令格式,也就是要加上%s作为URL的占位符,让模块知道怎么把目标地址传给Chrome。

给你两种可行的修正方案:

方案1:使用原始字符串(推荐)

在路径字符串前加r,让Python把反斜杠当成普通字符处理,同时加上%s占位符:

import webbrowser

chrome_path = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s"
webbrowser.get(chrome_path).open("https://google.com")

方案2:转义反斜杠

把每个\换成\\,同样加上%s:

import webbrowser

chrome_path = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe %s"
webbrowser.get(chrome_path).open("https://google.com")

另外提个小技巧:如果之后你把Chrome设为系统默认浏览器,直接用webbrowser.open("https://google.com")就会自动调用Chrome啦,不用额外指定路径~

内容的提问来源于stack exchange,提问作者alpha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 17:33:11