mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
修复同域iframe不自动断开问题
This commit is contained in:
parent
a704c12f38
commit
b291aa0c26
@ -121,8 +121,7 @@ class ChromiumDriver(object):
|
|||||||
except WebSocketTimeoutException:
|
except WebSocketTimeoutException:
|
||||||
continue
|
continue
|
||||||
except (WebSocketException, OSError, WebSocketConnectionClosedException):
|
except (WebSocketException, OSError, WebSocketConnectionClosedException):
|
||||||
if not self._stopped.is_set():
|
self.stop()
|
||||||
self.stop()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.debug:
|
if self.debug:
|
||||||
@ -150,8 +149,7 @@ class ChromiumDriver(object):
|
|||||||
try:
|
try:
|
||||||
self.event_handlers[event['method']](**event['params'])
|
self.event_handlers[event['method']](**event['params'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# pass
|
raise RuntimeError(f"\n回调函数错误:\n{e}")
|
||||||
raise RuntimeError(f"\n回调函数 {self.event_handlers[event['method']].__name__} 错误:\n{e}")
|
|
||||||
|
|
||||||
self.event_queue.task_done()
|
self.event_queue.task_done()
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
@Contact : g1879@qq.com
|
@Contact : g1879@qq.com
|
||||||
"""
|
"""
|
||||||
from re import search
|
from re import search
|
||||||
|
from threading import Thread
|
||||||
from time import sleep, perf_counter
|
from time import sleep, perf_counter
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
end_time = perf_counter() + 2
|
end_time = perf_counter() + 2
|
||||||
while perf_counter() < end_time and self.url == 'about:blank':
|
while perf_counter() < end_time and self.url == 'about:blank':
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
Thread(target=self._check_alive).start()
|
||||||
|
|
||||||
def __call__(self, loc_or_str, timeout=None):
|
def __call__(self, loc_or_str, timeout=None):
|
||||||
"""在内部查找元素
|
"""在内部查找元素
|
||||||
@ -109,8 +111,8 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
if self._debug:
|
if self._debug:
|
||||||
print('---获取document')
|
print('---获取document')
|
||||||
|
|
||||||
end_time = perf_counter() + 10
|
end_time = perf_counter() + 3
|
||||||
while perf_counter() < end_time:
|
while self.is_alive and perf_counter() < end_time:
|
||||||
try:
|
try:
|
||||||
if self._is_diff_domain is False:
|
if self._is_diff_domain is False:
|
||||||
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
|
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
|
||||||
@ -125,8 +127,8 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
except Exception:
|
except Exception:
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
else:
|
# else:
|
||||||
raise RuntimeError('获取document失败。')
|
# raise RuntimeError('获取document失败。')
|
||||||
|
|
||||||
if self._debug:
|
if self._debug:
|
||||||
print('---获取document结束')
|
print('---获取document结束')
|
||||||
@ -276,8 +278,8 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
return 'complete'
|
return 'complete'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
end_time = perf_counter() + 10
|
end_time = perf_counter() + 3
|
||||||
while perf_counter() < end_time:
|
while self.is_alive and perf_counter() < end_time:
|
||||||
try:
|
try:
|
||||||
return self.doc_ele.run_js('return this.readyState;')
|
return self.doc_ele.run_js('return this.readyState;')
|
||||||
except ContextLossError:
|
except ContextLossError:
|
||||||
@ -290,7 +292,12 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
|
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
raise RuntimeError('获取document失败。')
|
# raise RuntimeError('获取document失败。')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_alive(self):
|
||||||
|
"""返回是否仍可用"""
|
||||||
|
return self.states.is_alive
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scroll(self):
|
def scroll(self):
|
||||||
@ -588,6 +595,12 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
"""返回当前frame是否同域"""
|
"""返回当前frame是否同域"""
|
||||||
return self.frame_id in str(self.page.run_cdp('Page.getFrameTree')['frameTree'])
|
return self.frame_id in str(self.page.run_cdp('Page.getFrameTree')['frameTree'])
|
||||||
|
|
||||||
|
def _check_alive(self):
|
||||||
|
"""检测iframe是否有效线程方法"""
|
||||||
|
while self.is_alive:
|
||||||
|
sleep(1)
|
||||||
|
self.driver.stop()
|
||||||
|
|
||||||
# -------------准备废弃------------
|
# -------------准备废弃------------
|
||||||
def set_attr(self, attr, value):
|
def set_attr(self, attr, value):
|
||||||
"""设置frame元素attribute属性
|
"""设置frame元素attribute属性
|
||||||
|
@ -28,6 +28,8 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
loc_or_str: Union[Tuple[str, str], str],
|
loc_or_str: Union[Tuple[str, str], str],
|
||||||
timeout: float = None) -> Union[ChromiumElement, str]: ...
|
timeout: float = None) -> Union[ChromiumElement, str]: ...
|
||||||
|
|
||||||
|
def _check_alive(self) -> None: ...
|
||||||
|
|
||||||
def __repr__(self) -> str: ...
|
def __repr__(self) -> str: ...
|
||||||
|
|
||||||
def _runtime_settings(self) -> None: ...
|
def _runtime_settings(self) -> None: ...
|
||||||
@ -95,6 +97,9 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
@property
|
@property
|
||||||
def ready_state(self) -> str: ...
|
def ready_state(self) -> str: ...
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_alive(self) -> bool: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scroll(self) -> ChromiumFrameScroll: ...
|
def scroll(self) -> ChromiumFrameScroll: ...
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user