TabClosedError改为PageClosedError;修复iframe一个问题

This commit is contained in:
g1879 2023-11-15 19:51:16 +08:00
parent ee89ce948c
commit 66de3a6db1
3 changed files with 22 additions and 21 deletions

View File

@ -23,7 +23,7 @@ from .._units.network_listener import NetworkListener
from .._units.screencast import Screencast
from .._units.setter import ChromiumBaseSetter
from .._units.waiter import BaseWaiter
from ..errors import (ContextLossError, ElementLossError, CDPError, TabClosedError, NoRectError, AlertExistsError,
from ..errors import (ContextLossError, ElementLossError, CDPError, PageClosedError, NoRectError, AlertExistsError,
GetDocumentError)
@ -46,8 +46,6 @@ class ChromiumBase(BasePage):
self._listener = None
self._has_alert = False
self._ready_state = None
if self._debug:
print('在__init__变成None')
self._doc_got = False # 用于在LoadEventFired和FrameStoppedLoading间标记是否已获取doc
self._download_path = str(Path('.').absolute())
@ -130,6 +128,8 @@ class ChromiumBase(BasePage):
self._driver.set_callback('Page.frameDetached', self._onFrameDetached)
def _get_document(self):
if self._debug:
print('获取文档开始')
if self._is_reading:
return
self._is_reading = True
@ -150,6 +150,8 @@ class ChromiumBase(BasePage):
self._is_loading = False
self._is_reading = False
if self._debug:
print('获取文档结束')
def _onFrameDetached(self, **kwargs):
self.browser._frames.pop(kwargs['frameId'], None)
@ -285,7 +287,7 @@ class ChromiumBase(BasePage):
try:
self.run_cdp('Page.getLayoutMetrics')
return True
except TabClosedError:
except PageClosedError:
return False
@property
@ -435,7 +437,7 @@ class ChromiumBase(BasePage):
'No node found for given backend id'):
raise ElementLossError
elif error == 'tab closed':
raise TabClosedError
raise PageClosedError
elif error == 'timeout':
raise TimeoutError
elif error == 'alert exists.':
@ -680,7 +682,7 @@ class ChromiumBase(BasePage):
print('停止页面加载')
try:
self.run_cdp('Page.stopLoading')
except TabClosedError:
except PageClosedError:
pass
end_time = perf_counter() + self.timeouts.page_load
while self._ready_state != 'complete' and perf_counter() < end_time:

View File

@ -14,7 +14,7 @@ from .._units.rect import FrameRect
from .._units.scroller import FrameScroller
from .._units.setter import ChromiumFrameSetter
from .._units.waiter import FrameWaiter
from ..errors import ContextLossError, ElementLossError, GetDocumentError, TabClosedError
from ..errors import ContextLossError, ElementLossError, GetDocumentError, PageClosedError
class ChromiumFrame(ChromiumBase):
@ -103,14 +103,21 @@ class ChromiumFrame(ChromiumBase):
if debug:
print(f'{self._frame_id} reload 开始')
self._driver.stop()
try:
self._frame_ele = ChromiumElement(self._target_page, backend_id=self._backend_id)
except ElementLossError:
except (ElementLossError, PageClosedError):
return
node = self._target_page.run_cdp('DOM.describeNode',
backendNodeId=self._frame_ele.ids.backend_id)['node']
self._driver.stop()
end_time = perf_counter() + 2
while perf_counter() < end_time:
node = self._target_page.run_cdp('DOM.describeNode',
backendNodeId=self._frame_ele.ids.backend_id)['node']
if 'frameId' in node:
break
else:
return
if self._is_inner_frame():
self._is_diff_domain = False
@ -237,10 +244,6 @@ class ChromiumFrame(ChromiumBase):
if self._debug:
print(f'{self._frame_id}触发InspectorDetached')
try:
self._frame_ele.attrs
except (ElementLossError, TabClosedError):
self._driver.stop()
self._reload()
if self._debug:
@ -253,10 +256,6 @@ class ChromiumFrame(ChromiumBase):
if self._debug:
print(f'{self._frame_id}触发FrameDetached')
try:
self._frame_ele.attrs
except (ElementLossError, TabClosedError):
self._driver.stop()
self._reload()
if self._debug:

View File

@ -32,8 +32,8 @@ class CDPError(BaseError):
_info = '方法调用错误。'
class TabClosedError(BaseError):
_info = '标签页已关闭。'
class PageClosedError(BaseError):
_info = '已关闭。'
class ElementNotFoundError(BaseError):