xralf
xralf

Reputation: 3642

Close last opened tab

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.

Upvotes: 4

Views: 8050

Answers (2)

dexterx17
dexterx17

Reputation: 31

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

Upvotes: 3

aifarfa
aifarfa

Reputation: 3939

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.

Upvotes: 2

Related Questions