From bd47aee4cabcc33c4f328deacf680f11d7b12742 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 27 Dec 2023 07:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Driver=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/driver.py | 12 +++++++++--- DrissionPage/_base/driver.pyi | 2 ++ DrissionPage/_pages/chromium_frame.py | 12 +++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/DrissionPage/_base/driver.py b/DrissionPage/_base/driver.py index 46a52fb..eae9b8e 100644 --- a/DrissionPage/_base/driver.py +++ b/DrissionPage/_base/driver.py @@ -6,7 +6,7 @@ from json import dumps, loads, JSONDecodeError from queue import Queue, Empty from threading import Thread, Event -from time import perf_counter +from time import perf_counter, sleep from requests import get from websocket import (WebSocketTimeoutException, WebSocketConnectionClosedException, create_connection, @@ -104,7 +104,7 @@ class Driver(object): except WebSocketTimeoutException: continue except (WebSocketException, OSError, WebSocketConnectionClosedException, JSONDecodeError): - self.stop() + self._stop() return if self._debug: @@ -174,6 +174,13 @@ class Driver(object): return True def stop(self): + """中断连接""" + self._stop() + while self._handle_event_th.is_alive() or self._recv_th.is_alive(): + sleep(.1) + return True + + def _stop(self): """中断连接""" if self._stopped.is_set(): return False @@ -195,7 +202,6 @@ class Driver(object): self.event_handlers.clear() self.method_results.clear() self.event_queue.queue.clear() - return True def set_callback(self, event, callback, immediate=False): """绑定cdp event和回调方法 diff --git a/DrissionPage/_base/driver.pyi b/DrissionPage/_base/driver.pyi index 6ea93b4..2d23d91 100644 --- a/DrissionPage/_base/driver.pyi +++ b/DrissionPage/_base/driver.pyi @@ -54,6 +54,8 @@ class Driver(object): def stop(self) -> bool: ... + def _stop(self) -> None: ... + def set_callback(self, event: str, callback: Union[Callable, None], immediate: bool = False) -> None: ... def __str__(self) -> str: ... diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index b8f09cb..434942a 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -15,7 +15,7 @@ from .._units.scroller import FrameScroller from .._units.setter import ChromiumFrameSetter from .._units.states import FrameStates from .._units.waiter import FrameWaiter -from ..errors import ContextLostError, ElementLostError, GetDocumentError, PageClosedError, JavaScriptError +from ..errors import ContextLostError, ElementLostError, PageClosedError, JavaScriptError class ChromiumFrame(ChromiumBase): @@ -205,13 +205,19 @@ class ChromiumFrame(ChromiumBase): def _onInspectorDetached(self, **kwargs): """异域转同域或退出""" - self._reload() + try: + self._reload() + except PageClosedError: + pass def _onFrameDetached(self, **kwargs): """同域变异域""" self.browser._frames.pop(kwargs['frameId'], None) if kwargs['frameId'] == self._frame_id: - self._reload() + try: + self._reload() + except PageClosedError: + pass # ----------挂件----------