如何修改監聽異常的優先順序

花小田發表於2020-05-18

我想要的效果是 用例失敗時自動截圖,但是有些元素定位失敗,我是不希望 讓它截圖的,下面是模擬的程式碼

我希望 先輸出 “ 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()

相關文章