Selenium 如何修改监听异常的优先级

花小田 · 2020年05月18日 · 1344 次阅读

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

我希望 先输出 “ 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()
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册