1

I have a bit of a complicated selenium script but it contains an error response (I dont want to) handle because it will ruin another selenium command in the script, long story short I want to define

driver. close()

at the beginning of the script and call it in 2 minutes REGARDLESS.

def mytimer():
    time.sleep(120)
    driver.close()
mytimer()    

// Lots of selenium and webdriver stuff that will finish in 2 minutes because above as called a function that sleeps for 2 minutes and then calls driver.close()
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
mooman
  • 19
  • 4
  • Mind rephrasing the order of things you'd like to happen? Are you wanting to close out first then wait 2 minutes? Are you hinting you want to rest for 2 minutes to avoid potential throttling and you'd like your script to detect if throttling has been detected (in error response) then chill for 2 minutes before re-trying? – dnunez32 May 05 '20 at 17:26
  • @dnunez32 I want to run some selenium stuff in a python script, and I want "driver.close()" to be called at 2 minutes from the script starting, regardless if selenium hangs due to an error response – mooman May 05 '20 at 17:30
  • You could be thinking of a timer thread that runs along side your selenium script 2 minutes of you kicking off/running it. Check out this thread: https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds The post by Andrew Wilkins may help you out. After sleeping for 120 you'll do your driver.close(). – dnunez32 May 05 '20 at 17:34

1 Answers1

0

You could do that by executing the timer function in another thread. Example code:

from selenium import webdriver
import threading
from time import sleep

driver = webdriver.Chrome()
driver.get("url")

def mytimer():
    sleep(120)
    driver.close()

x = threading.Thread(target=mytimer)
x.start()

# rest of you code ...
muhmann
  • 191
  • 9