0

I have a Python Script B which is started by a subprocess.Popen call from my other Python Script A. This Python Script B runs a Selenium automation.

Now there are several cases that can occur while my subprocess B runs.

  1. The sricpt B finishes it's task at the end
  2. The script B crashes while executing
  3. The script A invokes a subprocess taskkill for script B

How can I now ensure that driver.quit() is executed in script B before any of these cases happen? So I think in case 1. It's pretty straight forward, you simply call driver.quit() at the end. In case 2. you could run a try-except-finally. Nevertheless, how would I ensure that driver.quit() is executed before/when the parent script A runs a taskkill command on B?

The taskkill is called in A.py as follows:

subprocess.call(['taskkill', '/F', '/T', '/PID',  str(processB.pid)])
Gordian
  • 101
  • 8

1 Answers1

0

Create your driver in Script A and pass it to script B to carry on with automation process. In script A place the driver in try & finally so that if the process completes successfully or fails, either way the driver must quit.

So something like:

Script A.

der runAutomation():
    driver = webdriver.Chrome('./chromedriver')
    try:
        ScriptB(driver)
    except:
        print("Something went wrong")
    finally:
        driver.quit()