【问题标题】:Do anyone know how to use python webbrowser.open under window有谁知道如何在window下使用python webbrowser.open
【发布时间】:2017-10-23 00:02:07
【问题描述】:

我曾尝试使用带有 webbrowser.open 的 python,但它只适用于 IE。如何让它打开chrome或firefox。我不希望它在 IE 上打开,我想在 Chrome 或 Firefox 上打开。由于我尝试了很多方法,但它们都不起作用。

import time
import webbrowser
webbrowser.open('www.google.com')

【问题讨论】:

标签: python google-chrome firefox webpage


【解决方案1】:

你需要指定你的webbrowser's name,详情见webbrowser.get

import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True

更新
如果您的计算机中安装了chromefirefox,请执行以下操作:

chrome_path =r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' # change to your chrome.exe path  
# webbrowser is just call subprocess.Popen, so make sure this work in your cmd firstly
# C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com

# there two way solve your problem
# you have change \ to / in windows
# this seems a bug in browser = shlex.split(browser) in windows
# ['C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe', '%s']
a = webbrowser.get(r'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe %s')
a.open('www.google.com')  #True
# or by register 
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'))
a = webbrowser.get('chrome')
a.open('www.google.com')  #True

否则你可以试试selenium,它提供了更多的功能,只需要chromedriver

【讨论】:

  • 我尝试使用它\,它会弹出:>>> a = webbrowser.get('firefox') Traceback(最近一次调用最后):文件“”,第 1 行,在 文件“C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\webbrowser.py”中,第 51 行,在 get raise Error("could not locate runnable browser") webbrowser .错误:找不到可运行的浏览器>>>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多