[英]invoke driver.quit() in all cases
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. 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.现在,当我的子流程 B 运行时,可能会发生几种情况。
How can I now ensure that driver.quit() is executed in script B before any of these cases happen?我现在如何确保 driver.quit() 在任何这些情况发生之前在脚本 B 中执行? So I think in case 1. It's pretty straight forward, you simply call driver.quit() at the end.所以我认为在案例 1 中。这很简单,您只需在最后调用 driver.quit() 即可。 In case 2. you could run a try-except-finally
.在情况 2. 您可以运行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?不过,我将如何确保在父脚本 A 在 B 上运行 taskkill 命令之前/时执行 driver.quit()?
The taskkill is called in A.py as follows: taskkill 在 A.py 中调用如下:
subprocess.call(['taskkill', '/F', '/T', '/PID', str(processB.pid)])
Create your driver in Script A and pass it to script B to carry on with automation process.在脚本 A 中创建您的驱动程序并将其传递给脚本 B 以继续进行自动化过程。 In script A place the driver in try & finally so that if the process completes successfully or fails, either way the driver must quit.在脚本 A 中,将驱动程序放在 try & finally 中,以便如果该过程成功完成或失败,无论哪种方式驱动程序都必须退出。
So something like:所以像:
Script A.脚本 A。
der runAutomation():
driver = webdriver.Chrome('./chromedriver')
try:
ScriptB(driver)
except:
print("Something went wrong")
finally:
driver.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.