主动连接也遵循加载策略超时;修复下载路径设置问题;增强读取doc稳定性

This commit is contained in:
g1879 2023-10-30 18:09:14 +08:00
parent 368665df57
commit 989a92adb7
6 changed files with 45 additions and 13 deletions

View File

@ -7,6 +7,7 @@ from json import loads, JSONDecodeError
from os.path import sep
from pathlib import Path
from re import findall
from threading import Thread
from time import perf_counter, sleep
from requests import get
@ -25,7 +26,7 @@ from .._units.screencast import Screencast
from .._units.setter import ChromiumBaseSetter
from .._units.waiter import ChromiumBaseWaiter
from ..errors import (ContextLossError, ElementLossError, CDPError, TabClosedError, NoRectError, BrowserConnectError,
AlertExistsError)
AlertExistsError, GetDocumentError)
class ChromiumBase(BasePage):
@ -126,8 +127,17 @@ class ChromiumBase(BasePage):
if self._is_reading:
return
self._is_reading = True
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId']
end_time = perf_counter() + 10
while perf_counter() < end_time:
try:
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId']
break
except:
continue
else:
raise GetDocumentError
r = self.run_cdp('Page.getFrameTree')
for i in findall(r"'id': '(.*?)'", str(r)):
self.browser._frames[i] = self.tab_id
@ -172,6 +182,10 @@ class ChromiumBase(BasePage):
if kwargs['frameId'] == self._frame_id:
self._ready_state = 'loading'
self._is_loading = True
if self.page_load_strategy == 'eager':
t = Thread(target=self._wait_to_stop)
t.daemon = True
t.start()
if self._debug:
print(f'frameStartedLoading {kwargs}')
@ -228,6 +242,14 @@ class ChromiumBase(BasePage):
"""
return self.ele(loc_or_str, timeout)
def _wait_to_stop(self):
"""eager策略超时时使页面停止加载"""
end_time = perf_counter() + self.timeouts.page_load
while perf_counter() < end_time:
sleep(.1)
if self._ready_state in ('interactive', 'complete') and self._is_loading:
self.stop_loading()
@property
def main(self):
return self._page

View File

@ -74,7 +74,7 @@ class ChromiumBase(BasePage):
def _onFileChooserOpened(self, **kwargs): ...
# def _onDownloadWillBegin(self, **kwargs): ...
def _wait_to_stop(self): ...
def _d_set_start_options(self, address, none) -> None: ...

View File

@ -149,14 +149,24 @@ class ChromiumFrame(ChromiumBase):
if self._is_reading:
return
self._is_reading = True
if self._is_diff_domain is False:
node = self._target_page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
self.doc_ele = ChromiumElement(self._target_page,
backend_id=node['contentDocument']['backendNodeId'])
end_time = perf_counter() + 10
while perf_counter() < end_time:
try:
if self._is_diff_domain is False:
node = self._target_page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
self.doc_ele = ChromiumElement(self._target_page,
backend_id=node['contentDocument']['backendNodeId'])
else:
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
self.doc_ele = ChromiumElement(self, backend_id=b_id)
break
except:
continue
else:
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
self.doc_ele = ChromiumElement(self, backend_id=b_id)
raise GetDocumentError
r = self.run_cdp('Page.getFrameTree')
for i in findall(r"'id': '(.*?)'", str(r)):

View File

@ -181,7 +181,7 @@ class BrowserDownloadManager(object):
return
mission.received_bytes = kwargs['receivedBytes']
mission.total_bytes = kwargs['totalBytes']
form_path = f'{mission.save_path}{sep}{mission.id}'
form_path = f'{mission.path}{sep}{mission.id}'
to_path = str(get_usable_path(f'{mission.path}{sep}{mission.name}'))
move(form_path, to_path)
self.set_done(mission, 'completed', final_path=to_path)

View File

@ -1,4 +1,4 @@
include DrissionPage/configs/configs.ini
include DrissionPage/_configs/configs.ini
include DrissionPage/*.pyi
include DrissionPage/*/*.py
include DrissionPage/*/*.pyi

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="4.0.0b1",
version="4.0.0b3",
author="g1879",
author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.",