繁体   English   中英

有谁知道如何在窗口下使用python webbrowser.open

[英]Do anyone know how to use python webbrowser.open under window

我曾尝试将webbrowser.open与python结合使用,但仅在IE上有效。 如何让它打开Chrome或Firefox。 我不想在IE上打开它,我想在Chrome或Firefox上打开它。 由于我尝试了许多方法,但是它们都不起作用。

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

您需要指定您的网络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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM