驱动程序关闭后杀死 python 脚本? 硒,蟒蛇

问题描述

在驱动程序关闭后杀死 python 脚本? (硒,蟒蛇) 因此,如果用户关闭了 chrome 驱动程序,脚本也会关闭。 这真的是全部,已经在谷歌上搜索了一段时间并没有找到任何东西。提前致谢

解决方法

您应该提供一些代码来帮助我们,但无论如何 chrome 驱动程序都在脚本附近,假设您使用 Selenium 进行测试,您可以使用 TearDown method 作为示例:

def tearDownClass(cls):
        super().tearDownClass()
        cls.browser.quit()
,
try:
    options = webdriver.ChromeOptions()
       
    browser.implicitly_wait(10)
    browser.get(url)
    #more code

except Exception as e:

    if "not reachable" in str(e):
        print("Chrome crashed :" + str(e))
        sys.exit()
    else:
        print("Some Other Exception was thrown: " + str(e))
        raise Exception(str(e))

只需捕获异常并使用 sys.exit()

这将为 chrome close 优雅地退出脚本,对于所有其他脚本,它将抛出该异常