我想要的效果是 用例失败时自动截图,但是有些元素定位失败,我是不希望 让它截图的,下面是模拟的代码

我希望 先输出 “ A,I should execute first”,然后输出 “B,I should execute last”,

程序输出的结果是 “ B,I should execute last”,然后输出 “ A,I should execute first”
有什么方法能实现呢

from selenium import webdriver
from selenium.webdriver.support.events import EventFiringWebDriver
from selenium.webdriver.support.events import AbstractEventListener


class ScreenshotListener(AbstractEventListener):
    def on_exception(self, exception, driver):
        print("B,I should execute last")


def test_demo():
    driver = webdriver.Chrome()
    d = EventFiringWebDriver(driver, ScreenshotListener())
    d.get("https://stackoverflow.com")
    try:
        d.find_element_by_css_selector("div.that-does-not-exist")
    except:
        print("A,I should execute first")



if __name__ == '__main__':
    test_demo()


↙↙↙阅读原文可查看相关链接,并与作者交流