问题
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