From dd5e48824ae11a94f101407f4698acd0b66c7811 Mon Sep 17 00:00:00 2001 From: linesight Date: Mon, 16 Nov 2026 22:34:21 -0800 Subject: [PATCH] Stop the click cycle in mouse_clicks.py snippet after the browser closes (#711) The snippet's delayed tasks reschedule each other and keep a reference to the browser. A task that was already posted can still run after the window has been closed (it runs while cef.Shutdown() pumps the message loop), at which point the browser is destroyed and SendMouseMoveEvent() raises "CefBrowser was destroyed". Set a flag in LifespanHandler.OnBeforeClose and have the tasks return without clicking or rescheduling once it is set. --- examples/snippets/mouse_clicks.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/snippets/mouse_clicks.py b/examples/snippets/mouse_clicks.py index b07d4fb2..5c329a09 100644 --- a/examples/snippets/mouse_clicks.py +++ b/examples/snippets/mouse_clicks.py @@ -2,6 +2,10 @@ from cefpython3 import cefpython as cef +# Set in OnBeforeClose. A delayed task that was already posted can still +# run after the browser is closed, and the browser can't be used then. +browser_closed = False + def main(): if not cef.Initialize(): @@ -18,6 +22,8 @@ def main(): def click_after_1_second(browser): + if browser_closed: + return print("Click after 1 second") # Mouse move to the top-left corner of the text browser.SendMouseMoveEvent(0, 70, False, 0) @@ -32,6 +38,8 @@ def click_after_1_second(browser): def click_after_2_seconds(browser): + if browser_closed: + return print("Click after 2 seconds") browser.SendMouseMoveEvent(0, 90, False, 0) browser.SendMouseClickEvent(0, 90, cef.MOUSEBUTTON_LEFT, False, 1) @@ -47,6 +55,10 @@ def OnLoadEnd(self, browser, **_): print("Page loading is complete") cef.PostDelayedTask(cef.TID_UI, 1000, click_after_1_second, browser) + def OnBeforeClose(self, browser, **_): + global browser_closed + browser_closed = True + if __name__ == '__main__': main()