diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 634dde2..1522d6b 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -1163,11 +1163,9 @@ class ChromiumBase(BasePage): self._tab_obj.Page.frameNavigated = self._onFrameNavigated self._tab_obj.Page.loadEventFired = self._onLoadEventFired - # self._tab_obj.DOM.documentUpdated = self._onDocumentUpdated def _get_document(self) -> None: """刷新cdp使用的document数据""" - # print('get doc') if not self._is_reading: self._is_reading = True self._wait_loading() @@ -1201,20 +1199,14 @@ class ChromiumBase(BasePage): def _onLoadEventFired(self, **kwargs): """在页面刷新、变化后重新读取页面内容""" - # print('load complete') if self._first_run is False and self._is_loading: self._get_document() def _onFrameNavigated(self, **kwargs): """页面跳转时触发""" if not kwargs['frame'].get('parentId', None): - # print('nav') self._is_loading = True - # def _onDocumentUpdated(self, **kwargs): - # # print('doc') - # pass - def _set_options(self) -> None: pass @@ -1253,7 +1245,7 @@ class ChromiumBase(BasePage): @property def url(self) -> str: """返回当前页面url""" - json = loads(self._control_session.get(f'http://{self.address}/json').text) + json = self._control_session.get(f'http://{self.address}/json').json() return [i['url'] for i in json if i['id'] == self._tab_obj.id][0] # change_mode要调用,不能用_driver @property @@ -1270,7 +1262,7 @@ class ChromiumBase(BasePage): @property def tab_id(self) -> str: """返回当前标签页id""" - return self._wait_driver.id + return self.driver.id if self.driver.status == 'started' else '' @property def ready_state(self) -> str: diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 2ad9b1f..4fd1ccc 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -1,5 +1,4 @@ # -*- coding:utf-8 -*- -from json import loads from pathlib import Path from platform import system from re import search @@ -51,11 +50,8 @@ class ChromiumPage(ChromiumBase): self.options = addr_tab_opts or DriverOptions() # 从ini文件读取 self.address = self.options.debugger_address self.process = connect_chrome(self.options)[1] - self._set_options() - json = loads(self._control_session.get(f'http://{self.address}/json').text) + json = self._control_session.get(f'http://{self.address}/json').json() tab_id = [i['id'] for i in json if i['type'] == 'page'][0] - self._init_page(tab_id) - self._get_document() # 接收浏览器地址和端口 elif isinstance(addr_tab_opts, str): @@ -63,12 +59,9 @@ class ChromiumPage(ChromiumBase): self.options = DriverOptions(read_file=False) self.options.debugger_address = addr_tab_opts self.process = connect_chrome(self.options)[1] - self._set_options() if not tab_id: - json = loads(self._control_session.get(f'http://{self.address}/json').text) + json = self._control_session.get(f'http://{self.address}/json').json() tab_id = [i['id'] for i in json if i['type'] == 'page'][0] - self._init_page(tab_id) - self._get_document() # 接收传递过来的Tab,浏览器 elif isinstance(addr_tab_opts, Tab): @@ -76,13 +69,13 @@ class ChromiumPage(ChromiumBase): self.address = search(r'ws://(.*?)/dev', addr_tab_opts._websocket_url).group(1) self.process = None self.options = DriverOptions(read_file=False) - self._set_options() - self._init_page(tab_id) - self._get_document() else: raise TypeError('只能接收Tab或DriverOptions类型参数。') + self._set_options() + self._init_page(tab_id) + self._get_document() self._first_run = False self.main_tab: str = self.tab_id @@ -111,7 +104,7 @@ class ChromiumPage(ChromiumBase): def tabs(self) -> list: """返回所有标签页id""" self._driver - json = loads(self._control_session.get(f'http://{self.address}/json').text) + json = self._control_session.get(f'http://{self.address}/json').json() return [i['id'] for i in json if i['type'] == 'page'] @property @@ -197,17 +190,29 @@ class ChromiumPage(ChromiumBase): """激活当前标签页使其处于最前面""" self._control_session.get(f'http://{self.address}/json/activate/{self.tab_id}') - def new_tab(self, url: str = None) -> None: - """新建并定位到一个标签页,该标签页在最后面 \n + def new_tab(self, url: str = None, switch_to: bool = True) -> None: + """新建并定位到一个标签页,该标签页在最后面 \n :param url: 新标签页跳转到的网址 + :param switch_to: 新建标签页后是否把焦点移过去 :return: None """ - begin_len = len(self.tabs) - url = f'?{url}' if url else '' - self._control_session.get(f'http://{self.address}/json/new{url}') - while len(self.tabs) < begin_len: - pass - self.to_tab(self.tabs[0]) + if switch_to: + begin_len = len(self.tabs) + self._control_session.get(f'http://{self.address}/json/new') + + tabs = self.tabs + while len(tabs) <= begin_len: + tabs = self.tabs + + self._to_tab(tabs[0], read_doc=False) + if url: + self.get(url) + + elif url: + self._control_session.get(f'http://{self.address}/json/new?{url}') + + else: + self._control_session.get(f'http://{self.address}/json/new') def to_main_tab(self) -> None: """跳转到主标签页""" @@ -219,6 +224,15 @@ class ChromiumPage(ChromiumBase): :param activate: 切换后是否变为活动状态 :return: None """ + self._to_tab(tab_id, activate) + + def _to_tab(self, tab_id: str = None, activate: bool = True, read_doc: bool = True) -> None: + """跳转到标签页 \n + :param tab_id: 标签页id字符串,默认跳转到main_tab + :param activate: 切换后是否变为活动状态 + :param read_doc: 切换后是否读取文档 + :return: None + """ tabs = self.tabs if not tab_id: tab_id = self.main_tab @@ -233,7 +247,7 @@ class ChromiumPage(ChromiumBase): self._driver.stop() self._init_page(tab_id) - if self.ready_state == 'complete': + if read_doc: self._get_document() def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = None, others: bool = False) -> None: diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index fa22947..ce9ac6f 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -41,8 +41,7 @@ class SessionPage(BasePage): :param data: session配置字典 :return: None """ - if self._session is None: - self._session = Session() + self._session = Session() if 'headers' in data: self._session.headers = CaseInsensitiveDict(data['headers']) diff --git a/setup.py b/setup.py index 22e624a..dd2e82d 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.0.10", + version="3.0.11", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",