修复监听一个问题;PageClosedError改为PageDisconnectedError

This commit is contained in:
g1879 2023-12-29 19:20:15 +08:00
parent 63266cce76
commit 6f5020f955
8 changed files with 22 additions and 24 deletions

View File

@ -8,7 +8,7 @@ from time import sleep, perf_counter
from .driver import BrowserDriver, Driver
from .._functions.tools import stop_process_on_port, raise_error
from .._units.downloader import DownloadManager
from ..errors import PageClosedError
from ..errors import PageDisconnectedError
__ERROR__ = 'error'
@ -143,7 +143,7 @@ class Browser(object):
:param tab_id: 标签页id
:return: None
"""
self.run_cdp('Target.closeTarget', targetId=tab_id, _ignore=PageClosedError)
self.run_cdp('Target.closeTarget', targetId=tab_id, _ignore=PageDisconnectedError)
def activate_tab(self, tab_id):
"""使标签页变为活动状态
@ -168,7 +168,7 @@ class Browser(object):
try:
self.run_cdp('Browser.close')
self.driver.stop()
except PageClosedError:
except PageDisconnectedError:
self.driver.stop()
return

View File

@ -12,7 +12,7 @@ from requests import get
from websocket import (WebSocketTimeoutException, WebSocketConnectionClosedException, create_connection,
WebSocketException)
from ..errors import PageClosedError
from ..errors import PageDisconnectedError
class Driver(object):
@ -77,7 +77,7 @@ class Driver(object):
except (OSError, WebSocketConnectionClosedException):
self.method_results.pop(ws_id, None)
return {'error': {'message': 'page closed'}, 'type': 'page_closed'}
return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'}
while not self._stopped.is_set():
try:
@ -96,7 +96,7 @@ class Driver(object):
continue
return {'error': 'page closed', 'type': 'page_closed'}
return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'}
def _recv_loop(self):
"""接收浏览器信息的守护线程方法"""
@ -159,7 +159,7 @@ class Driver(object):
:return: 执行结果
"""
if self._stopped.is_set():
return {'error': 'page closed', 'type': 'page_closed'}
return {'error': 'connection disconnected', 'type': 'connection_error'}
timeout = kwargs.pop('_timeout', 15)
result = self._send({'method': _method, 'params': kwargs}, timeout=timeout)
@ -258,5 +258,5 @@ class BrowserDriver(Driver):
def run_function(function, kwargs):
try:
function(**kwargs)
except PageClosedError:
except PageDisconnectedError:
pass

View File

@ -12,8 +12,8 @@ from time import perf_counter, sleep
from psutil import process_iter, AccessDenied, NoSuchProcess, ZombieProcess
from .._configs.options_manage import OptionsManager
from ..errors import (ContextLostError, ElementLostError, CDPError, PageClosedError, NoRectError, AlertExistsError,
WrongURLError, StorageError, CookieFormatError)
from ..errors import (ContextLostError, ElementLostError, CDPError, PageDisconnectedError, NoRectError,
AlertExistsError, WrongURLError, StorageError, CookieFormatError)
def get_usable_path(path, is_file=True, parents=True):
@ -267,8 +267,8 @@ def raise_error(result, ignore=None):
'No node with given id found', 'Node with given id does not belong to the document',
'No node found for given backend id'):
r = ElementLostError()
elif error in ('page closed', 'No target with given id found'):
r = PageClosedError()
elif error in ('connection disconnected', 'No target with given id found'):
r = PageDisconnectedError()
elif error == 'timeout':
r = TimeoutError(f'超时。\n错误:{result["error"]}\nmethod{result["method"]}\nargs{result["args"]}\n'
f'出现这个错误可能意味着程序有bug请把错误信息和重现方法告知作者谢谢。\n'

View File

@ -27,7 +27,7 @@ from .._units.scroller import PageScroller
from .._units.setter import ChromiumBaseSetter
from .._units.states import PageStates
from .._units.waiter import BaseWaiter
from ..errors import ContextLostError, CDPError, PageClosedError, ElementNotFoundError
from ..errors import ContextLostError, CDPError, PageDisconnectedError, ElementNotFoundError
__ERROR__ = 'error'
@ -672,7 +672,7 @@ class ChromiumBase(BasePage):
print('停止页面加载')
try:
self.run_cdp('Page.stopLoading')
except (PageClosedError, CDPError):
except (PageDisconnectedError, CDPError):
pass
end_time = perf_counter() + self.timeouts.page_load
while self._ready_state != 'complete' and perf_counter() < end_time:

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, PageClosedError, JavaScriptError
from ..errors import ContextLostError, ElementLostError, PageDisconnectedError, JavaScriptError
class ChromiumFrame(ChromiumBase):
@ -119,7 +119,7 @@ class ChromiumFrame(ChromiumBase):
else:
return
except (ElementLostError, PageClosedError):
except (ElementLostError, PageDisconnectedError):
return
if self._is_inner_frame():

View File

@ -295,9 +295,7 @@ class Listener(object):
packet = self._request_ids.get(rid)
if packet:
r = self._driver.run('Network.getResponseBody', requestId=rid)
if 'error' in r:
return
elif 'body' in r:
if 'body' in r:
packet._raw_body = r['body']
packet._base64_body = r['base64Encoded']
else:

View File

@ -4,7 +4,7 @@
@Contact : g1879@qq.com
"""
from .._functions.web import location_in_viewport
from ..errors import CDPError, NoRectError, PageClosedError, ElementLostError
from ..errors import CDPError, NoRectError, PageDisconnectedError, ElementLostError
class ElementStates(object):
@ -120,7 +120,7 @@ class PageStates(object):
try:
self._page.run_cdp('Page.getLayoutMetrics')
return True
except PageClosedError:
except PageDisconnectedError:
return False
@property
@ -152,7 +152,7 @@ class FrameStates(object):
try:
node = self._frame._target_page.run_cdp('DOM.describeNode',
backendNodeId=self._frame._frame_ele._backend_id)['node']
except (ElementLostError, PageClosedError):
except (ElementLostError, PageDisconnectedError):
return False
return 'frameId' in node

View File

@ -45,8 +45,8 @@ class CDPError(BaseError):
_info = '方法调用错误。'
class PageClosedError(BaseError):
_info = '页面已关闭'
class PageDisconnectedError(BaseError):
_info = '与页面的连接已断开'
class JavaScriptError(BaseError):