完善Driver逻辑

This commit is contained in:
g1879 2023-12-27 07:23:38 +08:00
parent 8c870c1c3e
commit bd47aee4ca
3 changed files with 20 additions and 6 deletions

View File

@ -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和回调方法

View File

@ -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: ...

View File

@ -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
# ----------挂件----------