如何使用python关闭Web浏览器

Pis*_*usa 10 python python-webbrowser

我已经webbrowser.open(url)在 python 中打开了一个 webbrowser 窗口,现在我想关闭使用 python 打开的 webbrowser。有可能这样做吗?

小智 9

没有 webbrowser.close,你可以使用这些代码来关闭任务(在 Windows 操作系统中):

首先导入 os 包

import os
Run Code Online (Sandbox Code Playgroud)

然后使用系统函数杀死任务

os.system("taskkill /im firefox.exe /f")
os.system("taskkill /im chrome.exe /f")
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 MacOS,您可以使用以下命令:

os.system("killall -9 'Google Chrome'")
Run Code Online (Sandbox Code Playgroud)