From f278a32ede0a9f2c5225cb4f30db8c3d101cf4cf Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 1 Nov 2023 02:02:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dframe=5Fid=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 ++ DrissionPage/_base/browser.py | 5 ++--- DrissionPage/_base/chromium_driver.py | 12 +++++++----- DrissionPage/_pages/chromium_base.py | 10 +++++----- DrissionPage/_pages/chromium_frame.py | 2 ++ 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 8a2eaf3..ad912d6 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -11,3 +11,5 @@ from ._pages.web_page import WebPage # 启动配置类 from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions + +__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage'] diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 1d814cb..4c7aee0 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -84,9 +84,8 @@ class Browser(object): @property def tabs(self): """返回所有标签页id组成的列表""" - # j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp - j = self.run_cdp('Target.getTargets')['targetInfos'] - return [i['targetId'] for i in j if i['type'] == 'page'] + j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp + return [i['id'] for i in j if i['type'] == 'page'] @property def process_id(self): diff --git a/DrissionPage/_base/chromium_driver.py b/DrissionPage/_base/chromium_driver.py index 6732dc6..a8181e6 100644 --- a/DrissionPage/_base/chromium_driver.py +++ b/DrissionPage/_base/chromium_driver.py @@ -137,11 +137,11 @@ class ChromiumDriver(object): function = self.event_handlers.get(event['method']) if function: - if self._debug: - print(f'开始执行 {function.__name__}') + # if self._debug: + # print(f'开始执行 {function.__name__}') function(**event['params']) - if self._debug: - print(f'执行 {function.__name__}完毕') + # if self._debug: + # print(f'执行 {function.__name__}完毕') self.event_queue.task_done() @@ -225,4 +225,6 @@ class BrowserDriver(ChromiumDriver): return f"" def get(self, url): - return get(url, headers={'Connection': 'close'}) + r = get(url, headers={'Connection': 'close'}) + r.close() + return r diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 2afb42f..2225a44 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -95,17 +95,12 @@ class ChromiumBase(BasePage): self._get_document() self._ready_state = 'complete' - r = self.run_cdp('Page.getFrameTree') - for i in findall(r"'id': '(.*?)'", str(r)): - self.browser._frames[i] = self.tab_id - def _driver_init(self, tab_id): """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 :param tab_id: 要跳转到的标签页id :return: None """ self._is_loading = True - self._frame_id = tab_id self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address) self._alert = Alert() self._driver.set_listener('Page.javascriptDialogOpening', self._on_alert_open) @@ -123,6 +118,11 @@ class ChromiumBase(BasePage): self._driver.set_listener('Page.frameAttached', self._onFrameAttached) self._driver.set_listener('Page.frameDetached', self._onFrameDetached) + r = self.run_cdp('Page.getFrameTree') + for i in findall(r"'id': '(.*?)'", str(r)): + self.browser._frames[i] = self.tab_id + self._frame_id = r['frameTree']['frame']['id'] + def _get_document(self): if self._is_reading: return diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 5613f58..aeba255 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -46,6 +46,7 @@ class ChromiumFrame(ChromiumBase): self._is_diff_domain = False self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId']) super().__init__(page.address, page.tab_id, page.timeout) + self._frame_id = self.frame_id else: self._is_diff_domain = True super().__init__(page.address, self.frame_id, page.timeout) @@ -113,6 +114,7 @@ class ChromiumFrame(ChromiumBase): self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId']) super().__init__(self.address, self._target_page.tab_id, self._target_page.timeout) + self._frame_id = self.frame_id self._debug = debug self.driver._debug = d_debug else: