From 4a9ef7d6b2b3511a353da2a33fb1205d83bef696 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 12 Mar 2024 23:41:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dset=5Fflags=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=97=AE=E9=A2=98=EF=BC=9B=E4=BF=AE=E6=94=B9quit()?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 35 +++++++++++++++++----------- DrissionPage/_functions/browser.py | 4 ++-- DrissionPage/_pages/chromium_base.py | 2 +- DrissionPage/_pages/chromium_tab.py | 2 +- DrissionPage/_pages/session_page.py | 2 +- DrissionPage/_pages/web_page.py | 2 +- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index caa04d2..4bee1aa 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -203,24 +203,31 @@ class Browser(object): :param force: 是否立刻强制终止进程 :return: None """ - pids = [pid['id'] for pid in self.run_cdp('SystemInfo.getProcessInfo')['processInfo']] - for tab in self._all_drivers.values(): + try: + self.run_cdp('Browser.close') + except PageDisconnectedError: + pass + self.driver.stop() + + drivers = list(self._all_drivers.values()) + for tab in drivers: for driver in tab: driver.stop() - if force: - from psutil import Process - for pid in pids: - try: - Process(pid).kill() - except: - pass - else: + if not force: + return + + try: + pids = [pid['id'] for pid in self.run_cdp('SystemInfo.getProcessInfo')['processInfo']] + except: + return + + from psutil import Process + for pid in pids: try: - self.run_cdp('Browser.close') - self.driver.stop() - except PageDisconnectedError: - self.driver.stop() + Process(pid).kill() + except: + pass from os import popen from platform import system diff --git a/DrissionPage/_functions/browser.py b/DrissionPage/_functions/browser.py index a707c61..ccc78fc 100644 --- a/DrissionPage/_functions/browser.py +++ b/DrissionPage/_functions/browser.py @@ -176,8 +176,8 @@ def set_flags(opt): states_dict = load(f) except JSONDecodeError: states_dict = {} - flags_list = [] if opt.clear_file_flags else states_dict.setdefault( - 'browser', {}).setdefault('enabled_labs_experiments', []) + states_dict.setdefault('browser', {}).setdefault('enabled_labs_experiments', []) + flags_list = [] if opt.clear_file_flags else states_dict['browser']['enabled_labs_experiments'] flags_dict = {} for i in flags_list: f = str(i).split('@', 1) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 153ba75..3d4a6fc 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -474,7 +474,7 @@ class ChromiumBase(BasePage): def cookies(self, as_dict=False, all_domains=False, all_info=False): """返回cookies信息 - :param as_dict: 为True时返回由{name: value}键值对组成的dict,为True时返回list且all_info无效 + :param as_dict: 为True时以dict格式返回,为False时返回list且all_info无效 :param all_domains: 是否返回所有域的cookies :param all_info: 是否返回所有信息,为False时只返回name、value、domain :return: cookies信息 diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 23de4be..fa0fb06 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -363,7 +363,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): def cookies(self, as_dict=False, all_domains=False, all_info=False): """返回cookies - :param as_dict: 是否以字典方式返回 + :param as_dict: 为True时以dict格式返回,为False时返回list且all_info无效 :param all_domains: 是否返回所有域的cookies :param all_info: 是否返回所有信息,False则只返回name、value、domain :return: cookies信息 diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index d0e81a3..0fa1691 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -221,7 +221,7 @@ class SessionPage(BasePage): def cookies(self, as_dict=False, all_domains=False, all_info=False): """返回cookies - :param as_dict: 是否以字典方式返回,False则以list返回 + :param as_dict: 为True时以dict格式返回,为False时返回list且all_info无效 :param all_domains: 是否返回所有域的cookies :param all_info: 是否返回所有信息,False则只返回name、value、domain :return: cookies信息 diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 8253e40..6873b77 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -298,7 +298,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def cookies(self, as_dict=False, all_domains=False, all_info=False): """返回cookies - :param as_dict: 是否以字典方式返回,False以list形式返回 + :param as_dict: 为True时以dict格式返回,为False时返回list且all_info无效 :param all_domains: 是否返回所有域的cookies :param all_info: 是否返回所有信息,False则只返回name、value、domain :return: cookies信息