Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close last opened tab

Tags:

python

browser

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.

like image 584
xralf Avatar asked Sep 06 '25 16:09

xralf


2 Answers

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

like image 165
dexterx17 Avatar answered Sep 10 '25 00:09

dexterx17


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.

like image 28
aifarfa Avatar answered Sep 10 '25 00:09

aifarfa