Clean way in Python to open Windows default web browser with a URL and then close it after a few seconds

100 Views Asked by At

As the title suggests I want to find a way in Python to do that. I have tried:

1) Selenium + Webdriver : I don't like the dependencies, like a user need to install a webdriver, tried bypassing it with webdriver-manager module but still there was some issues cause I don't know for sure that a user might have Chrome installed, or Firefox etc...so I don't know what webdriver to choose. Also speed was not great in this case.

2) webbrowser module: Very fast way to open the default browser at a specific URL. Can't control if it opens in a new window or in a new tab (I know there is a open_new method but couldn't get it to work). Main problem is I can't close the tab/window.

3) subprocesses module: p = subprocess.Popen(f'{path_to_browser} https://www.reddit.com/') I have found a way to list browsers installed in a system using the pybrowsers module so path_to_variable is determined at runtime. I can't seem to kill the process though...

I have tried p.kill() or os.system("taskkill /F /pid "+str(p.pid)) or os.kill(p.pid, signal.SIGILL) but nothing works cause I believe p.pid is not the process that I need to kill (it has spawned a bunch of other processes)

Is there a clean way to do this kind of thing? I could always do os.system("taskkill /im {browser.exe} /f") but this will terminate all open tabs...

0

There are 0 best solutions below