From 1d4b6290acf2324d183d710442f9877f44ded304 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 28 Nov 2022 17:34:50 +0800 Subject: [PATCH] =?UTF-8?q?WebPage=E5=A2=9E=E5=8A=A0quit()=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DMixPage=E7=9A=84download=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 7 +++++++ DrissionPage/mix_page.py | 1 - DrissionPage/web_page.py | 26 +++++++++++++++++++++++++- setup.py | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 348607b..23d3dc7 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -335,6 +335,13 @@ class ChromiumBase(BasePage): result_cookies.append(c) self._wait_driver.Network.setCookies(cookies=result_cookies) + # def set_headers(self, headers: dict) -> None: + # """设置固定发送的headers \n + # :param headers: dict格式的headers数据 + # :return: None + # """ + # self.run_cdp('Network.setExtraHTTPHeaders', headers=headers) + def ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'], timeout: float = None) -> Union[ChromiumElement, 'ChromiumFrame', None]: diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index 4d79369..fbde471 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -53,7 +53,6 @@ class MixPage(SessionPage, DriverPage, BasePage): self._wait_object = None self._response = None self._scroll = None - self._download_kit = None if self._mode == 'd': try: diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 4a5dd81..e9d2b65 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -317,6 +317,16 @@ class WebPage(SessionPage, ChromiumPage, BasePage): if set_session: super().set_cookies(cookies) + # def set_headers(self, headers: dict) -> None: + # """设置固定发送的headers \n + # :param headers: dict格式的headers数据 + # :return: None + # """ + # if self._mode == 's': + # self.session.headers = headers + # elif self._mode == 'd': + # super(SessionPage, self).set_headers(headers) + def check_page(self, by_requests: bool = False) -> Union[bool, None]: """d模式时检查网页是否符合预期 \n 默认由response状态检查,可重载实现针对性检查 \n @@ -346,6 +356,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): """关闭session""" if self._has_session: self.change_mode('d') + self._session.close() self._session = None self._response = None self._has_session = None @@ -378,7 +389,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def _ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], - timeout: float = None, single: bool = True, relative:bool=False) \ + timeout: float = None, single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, SessionElement, ChromiumFrame, str, None, List[Union[SessionElement, str]], List[ Union[ChromiumElement, str, ChromiumFrame]]]: """返回页面中符合条件的元素、属性或节点文本,默认返回第一个 \n @@ -430,3 +441,16 @@ class WebPage(SessionPage, ChromiumPage, BasePage): else: raise TypeError('session_or_options参数只能接收Session, dict, SessionOptions或False。') + + def quit(self) -> None: + """关闭浏览器,关闭session""" + if self._has_session: + self._session.close() + self._session = None + self._response = None + self._has_session = None + if self._has_driver: + self._tab_obj.Browser.close() + self._tab_obj.stop() + self._tab_obj = None + self._has_driver = None diff --git a/setup.py b/setup.py index 14eea8b..ec623c7 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.15", + version="3.0.16", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",