简体   繁体   English

在所有情况下调用 driver.quit()

[英]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 运行时,可能会发生几种情况。

  1. The sricpt B finishes it's task at the end sricpt B 最后完成了它的任务
  2. The script B crashes while executing脚本 B 在执行时崩溃
  3. The script A invokes a subprocess taskkill for script B脚本 A 为脚本 B 调用子进程 taskkill

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.

相关问题 在 driver.quit 后调用 chromedriver 不起作用? - Invoking chromedriver after driver.quit not working? Selenium ChromeDriver-driver.quit()上的HTTP 407 - Selenium ChromeDriver - HTTP 407 on driver.quit() driver.quit() 在未处理的异常之后 - driver.quit() after unhandled exception python selenium driver.quit() 里面除了块 - python selenium driver.quit() inside except block python selenium driver.quit() 不会中途终止程序 - python selenium driver.quit() won't terminate the program midway 如果设置了firefox_profile,则python selenium driver.quit()无法退出firefox浏览器 - python selenium driver.quit() can not quit firefox browser if firefox_profile setted Selenium 中的 driver.quit():ConnectionRefusedError 如果用于多个驱动程序 object - driver.quit() in Selenium : ConnectionRefusedError if used on more than one driver object 尽管 driver.close 和 driver.quit,IDLE 不会终止我的 selenium 浏览器 - IDLE doesn't terminate my selenium broswer despite driver.close and driver.quit driver.quit() 和 pkill chrome 释放内存的效果一样吗? - Do driver.quit() and pkill chrome have the same effect in releasing memory? 硒3.0.2中的driver.quit firefox 50.1.0 firefox已停止工作 - driver.quit in selenium 3.0.2 firefox 50.1.0 firefox has stopped working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM