-1

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)
d219
  • 2,707
  • 5
  • 31
  • 36

3 Answers3

3

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()
Pygirl
  • 12,969
  • 5
  • 30
  • 43
1

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

fesieg
  • 467
  • 1
  • 4
  • 14
1

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
Berry
  • 61
  • 1
  • 4