From af6a759b4ad5acf748af613a177c59a687d36357 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 28 Nov 2023 17:44:03 +0800 Subject: [PATCH] =?UTF-8?q?quit()=E7=9A=84force=E5=8F=82=E6=95=B0=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=AB=8B=E5=8D=B3=E6=89=A7=E8=A1=8C=EF=BC=9B=E5=85=B6?= =?UTF-8?q?=E5=AE=83=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_base/browser.py | 17 +++++++++-------- DrissionPage/_base/browser.pyi | 2 +- DrissionPage/_configs/chromium_options.py | 2 +- DrissionPage/_configs/configs.ini | 1 - DrissionPage/_pages/chromium_base.py | 1 - DrissionPage/_pages/chromium_page.py | 3 +-- DrissionPage/_pages/web_page.py | 3 +-- setup.py | 2 +- 9 files changed, 15 insertions(+), 18 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 6be91ef..0b0732b 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.0b13' +__version__ = '4.0.0b15' diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 3fdabcd..7f882c0 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -156,15 +156,22 @@ class Browser(object): """ return self.run_cdp('Browser.getWindowForTarget', targetId=tab_id or self.id)['bounds'] - def quit(self, timeout=5, force=True): + def quit(self, timeout=5, force=False): """关闭浏览器 :param timeout: 等待浏览器关闭超时时间 - :param force: 关闭超时是否强制终止进程 + :param force: 是否立刻强制终止进程 :return: None """ self.run_cdp('Browser.close') self.driver.stop() + if force: + ip, port = self.address.split(':') + if ip not in ('127.0.0.1', 'localhost'): + return + stop_process_on_port(port) + return + if self.process_id: from os import popen from platform import system @@ -180,11 +187,5 @@ class Browser(object): except TypeError: pass - if force: - ip, port = self.address.split(':') - if ip not in ('127.0.0.1', 'localhost'): - return - stop_process_on_port(port) - def _on_quit(self): Browser.BROWSERS.pop(self.id, None) diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index 190662f..963e3f7 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -57,6 +57,6 @@ class Browser(object): def _onTargetDestroyed(self, **kwargs) -> None: ... - def quit(self, timeout: float = 5, force: bool = True) -> None: ... + def quit(self, timeout: float = 5, force: bool = False) -> None: ... def _on_quit(self) -> None: ... diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 9ec74b0..fdd2edf 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -40,7 +40,7 @@ class ChromiumOptions(object): self._load_mode = options.get('load_mode', 'normal') self._proxy = om.proxies.get('http', None) self._system_user_path = options.get('system_user_path', False) - self._existing_only = options.get('is_existing_only', False) + self._existing_only = options.get('existing_only', False) user_path = user = False for arg in self._arguments: diff --git a/DrissionPage/_configs/configs.ini b/DrissionPage/_configs/configs.ini index 72722bd..3f0645f 100644 --- a/DrissionPage/_configs/configs.ini +++ b/DrissionPage/_configs/configs.ini @@ -12,7 +12,6 @@ load_mode = normal user = Default auto_port = False system_user_path = False -is_existing_only = False existing_only = False [session_options] diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 3e67165..74320fc 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -165,7 +165,6 @@ class ChromiumBase(BasePage): return True except: - print('获取文档失败') if self._debug: print('获取文档失败') return False diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 5c825be..5eb8158 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -27,8 +27,7 @@ class ChromiumPage(ChromiumBase): :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间 """ - if not addr_or_opts and addr_driver_opts: - addr_or_opts = addr_driver_opts + addr_or_opts = addr_or_opts or addr_driver_opts self._page = self address = self._handle_options(addr_or_opts) self._run_browser() diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 339436d..eaca23b 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -22,8 +22,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :param chromium_options: ChromiumDriver对象,只使用s模式时应传入False :param session_or_options: Session对象或SessionOptions对象,只使用d模式时应传入False """ - if not chromium_options and driver_or_options: - chromium_options = driver_or_options + chromium_options = chromium_options or driver_or_options self._mode = mode.lower() if self._mode not in ('s', 'd'): raise ValueError('mode参数只能是s或d。') diff --git a/setup.py b/setup.py index 33e5c2b..7b3053d 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="4.0.0b13", + version="4.0.0b15", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",