How to close the existing browser tab using the Python webbrowser package

可紊 提交于 2019-12-08 14:58:23

问题


Using Python webbrowser package I can open a new tab with a specified URL. Is there a way to close this tab? I referred the below official docs and nothing related to close action is mentioned.

Python webbrowser package doc: https://docs.python.org/3/library/webbrowser.html


回答1:


No, webbrowser doesn't have methods to close tabs nor browser itself as you may see in its documentation page: https://docs.python.org/2/library/webbrowser.html




回答2:


You could

Use a hotkey using the pykeyboard library which you can read about at https://github.com/SavinaRoja/PyUserInput

or the keyboard library at https://github.com/boppreh/keyboard

Another (but probably worse) option is:

You may use a "taskkill" command like

import os    
os.system("taskkill /im chrome.exe /f")

However, this will just kill all processes and close the chosen program (in this case chrome.exe)

(This also may delete data from the browser, f.eks. you lose all you're windows even tho you have chosen in settings to save them for next time you open your browser)



来源:https://stackoverflow.com/questions/30326196/how-to-close-the-existing-browser-tab-using-the-python-webbrowser-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!