修复frame_id不对应问题

This commit is contained in:
g1879 2023-11-01 02:02:59 +08:00
parent 5f0ef46b93
commit f278a32ede
5 changed files with 18 additions and 13 deletions

View File

@ -11,3 +11,5 @@ from ._pages.web_page import WebPage
# 启动配置类 # 启动配置类
from ._configs.chromium_options import ChromiumOptions from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage']

View File

@ -84,9 +84,8 @@ class Browser(object):
@property @property
def tabs(self): def tabs(self):
"""返回所有标签页id组成的列表""" """返回所有标签页id组成的列表"""
# j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp
j = self.run_cdp('Target.getTargets')['targetInfos'] return [i['id'] for i in j if i['type'] == 'page']
return [i['targetId'] for i in j if i['type'] == 'page']
@property @property
def process_id(self): def process_id(self):

View File

@ -137,11 +137,11 @@ class ChromiumDriver(object):
function = self.event_handlers.get(event['method']) function = self.event_handlers.get(event['method'])
if function: if function:
if self._debug: # if self._debug:
print(f'开始执行 {function.__name__}') # print(f'开始执行 {function.__name__}')
function(**event['params']) function(**event['params'])
if self._debug: # if self._debug:
print(f'执行 {function.__name__}完毕') # print(f'执行 {function.__name__}完毕')
self.event_queue.task_done() self.event_queue.task_done()
@ -225,4 +225,6 @@ class BrowserDriver(ChromiumDriver):
return f"<BrowserDriver {self.id}>" return f"<BrowserDriver {self.id}>"
def get(self, url): def get(self, url):
return get(url, headers={'Connection': 'close'}) r = get(url, headers={'Connection': 'close'})
r.close()
return r

View File

@ -95,17 +95,12 @@ class ChromiumBase(BasePage):
self._get_document() self._get_document()
self._ready_state = 'complete' 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): def _driver_init(self, tab_id):
"""新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化
:param tab_id: 要跳转到的标签页id :param tab_id: 要跳转到的标签页id
:return: None :return: None
""" """
self._is_loading = True self._is_loading = True
self._frame_id = tab_id
self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address) self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address)
self._alert = Alert() self._alert = Alert()
self._driver.set_listener('Page.javascriptDialogOpening', self._on_alert_open) 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.frameAttached', self._onFrameAttached)
self._driver.set_listener('Page.frameDetached', self._onFrameDetached) 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): def _get_document(self):
if self._is_reading: if self._is_reading:
return return

View File

@ -46,6 +46,7 @@ class ChromiumFrame(ChromiumBase):
self._is_diff_domain = False self._is_diff_domain = False
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId']) self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
super().__init__(page.address, page.tab_id, page.timeout) super().__init__(page.address, page.tab_id, page.timeout)
self._frame_id = self.frame_id
else: else:
self._is_diff_domain = True self._is_diff_domain = True
super().__init__(page.address, self.frame_id, page.timeout) super().__init__(page.address, self.frame_id, page.timeout)
@ -113,6 +114,7 @@ class ChromiumFrame(ChromiumBase):
self.doc_ele = ChromiumElement(self._target_page, self.doc_ele = ChromiumElement(self._target_page,
backend_id=node['contentDocument']['backendNodeId']) backend_id=node['contentDocument']['backendNodeId'])
super().__init__(self.address, self._target_page.tab_id, self._target_page.timeout) super().__init__(self.address, self._target_page.tab_id, self._target_page.timeout)
self._frame_id = self.frame_id
self._debug = debug self._debug = debug
self.driver._debug = d_debug self.driver._debug = d_debug
else: else: