quit()的force参数改为立即执行;其它微调

This commit is contained in:
g1879 2023-11-28 17:44:03 +08:00
parent ddd7aba9ae
commit af6a759b4a
9 changed files with 15 additions and 18 deletions

View File

@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.0b13' __version__ = '4.0.0b15'

View File

@ -156,15 +156,22 @@ class Browser(object):
""" """
return self.run_cdp('Browser.getWindowForTarget', targetId=tab_id or self.id)['bounds'] 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 timeout: 等待浏览器关闭超时时间
:param force: 关闭超时是否强制终止进程 :param force: 是否立刻强制终止进程
:return: None :return: None
""" """
self.run_cdp('Browser.close') self.run_cdp('Browser.close')
self.driver.stop() 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: if self.process_id:
from os import popen from os import popen
from platform import system from platform import system
@ -180,11 +187,5 @@ class Browser(object):
except TypeError: except TypeError:
pass 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): def _on_quit(self):
Browser.BROWSERS.pop(self.id, None) Browser.BROWSERS.pop(self.id, None)

View File

@ -57,6 +57,6 @@ class Browser(object):
def _onTargetDestroyed(self, **kwargs) -> None: ... 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: ... def _on_quit(self) -> None: ...

View File

@ -40,7 +40,7 @@ class ChromiumOptions(object):
self._load_mode = options.get('load_mode', 'normal') self._load_mode = options.get('load_mode', 'normal')
self._proxy = om.proxies.get('http', None) self._proxy = om.proxies.get('http', None)
self._system_user_path = options.get('system_user_path', False) 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 user_path = user = False
for arg in self._arguments: for arg in self._arguments:

View File

@ -12,7 +12,6 @@ load_mode = normal
user = Default user = Default
auto_port = False auto_port = False
system_user_path = False system_user_path = False
is_existing_only = False
existing_only = False existing_only = False
[session_options] [session_options]

View File

@ -165,7 +165,6 @@ class ChromiumBase(BasePage):
return True return True
except: except:
print('获取文档失败')
if self._debug: if self._debug:
print('获取文档失败') print('获取文档失败')
return False return False

View File

@ -27,8 +27,7 @@ class ChromiumPage(ChromiumBase):
:param tab_id: 要控制的标签页id不指定默认为激活的 :param tab_id: 要控制的标签页id不指定默认为激活的
:param timeout: 超时时间 :param timeout: 超时时间
""" """
if not addr_or_opts and addr_driver_opts: addr_or_opts = addr_or_opts or addr_driver_opts
addr_or_opts = addr_driver_opts
self._page = self self._page = self
address = self._handle_options(addr_or_opts) address = self._handle_options(addr_or_opts)
self._run_browser() self._run_browser()

View File

@ -22,8 +22,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
:param chromium_options: ChromiumDriver对象只使用s模式时应传入False :param chromium_options: ChromiumDriver对象只使用s模式时应传入False
:param session_or_options: Session对象或SessionOptions对象只使用d模式时应传入False :param session_or_options: Session对象或SessionOptions对象只使用d模式时应传入False
""" """
if not chromium_options and driver_or_options: chromium_options = chromium_options or driver_or_options
chromium_options = driver_or_options
self._mode = mode.lower() self._mode = mode.lower()
if self._mode not in ('s', 'd'): if self._mode not in ('s', 'd'):
raise ValueError('mode参数只能是s或d。') raise ValueError('mode参数只能是s或d。')

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="4.0.0b13", version="4.0.0b15",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.", description="Python based web automation tool. It can control the browser and send and receive data packets.",