I want to open a URL for 1 minute and then close it automatically after the 1 minute has passed using Python, how can I do that it please?
import time
import webbrowser
open('https://www.example.com')
time.sleep(5)
I want to open a URL for 1 minute and then close it automatically after the 1 minute has passed using Python, how can I do that it please?
import time
import webbrowser
open('https://www.example.com')
time.sleep(5)
Using Subprocess
:
import time
import subprocess
p = subprocess.Popen(["firefox", "https://www.example.com"])
time.sleep(60)
p.kill()
Using selenium
:
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://www.example.com")
sleep(60)
driver.close()
The webbrowser module is not great for this. What you could do is kill the process started by open
, but urllib
is the better way to handle these things
You can try using keyboard shortcuts:
import keyboard
import time
import os
os.system('start https://www.example.com')
time.sleep(60) # note I changed this from 5 to 60 seconds
keyboard.press_and_release('ctrl+w') # closes the last tab opened