Close last opened tab

烈酒焚心 提交于 2019-12-24 00:43:32

问题


Can I close last opened tab (without closing the browser) from Python that was opened with the following code?

import webbrowser
webbrowser.get("firefox").open_new_tab(url)

You can use whatever for this tasks. I know that webbrowser module is not able to do it.


回答1:


No, you can't close browser programmatically(without hacking or create plug-in).

the browser controller only provide methods to open browser but not to close.

this implicitly call new Process then parse command-line arguments such as

import subprocess
subprocess.Popen("firefox -new-tab %s" % url, shell=True)

equal to open shell cmd:

C:\Program Files\Mozilla Firefox\firefox -new-tab http://docs.python.org

also most standard browser include Firefox provided its command line args to open new windows/tab but nothing to close opened tab.




回答2:


You can send the hotkey combination to close the tab (Ctrl + W) using the pykeyboard library from here, https://github.com/SavinaRoja/PyUserInput.



来源:https://stackoverflow.com/questions/10449242/close-last-opened-tab

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