From 4f32553a3cf9a4c1ba2ca749bf462ba2c72153b6 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 12 Apr 2021 17:50:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84kill=5Fbrowser()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/drission.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 217fb0a..e15f87b 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -219,10 +219,35 @@ class Drission(object): """调试浏览器进程""" return self._debugger - def kill_browser(self): + def kill_browser(self) -> None: """关闭浏览器进程(如果可以)""" if self.debugger_progress: self.debugger_progress.kill() + return + + address = self.driver_options.get('debugger_address', '').split(':') + if len(address) == 1: + self.close_driver() + + elif len(address) == 2: + ip, port = address + if ip not in ('127.0.0.1', 'localhost') or not port.isdigit(): + return + + from os import popen + progresses = popen(f'netstat -nao | findstr :{port}').read().split('\n') + txt = '' + for progress in progresses: + if 'LISTENING' in progress: + txt = progress + break + + if not txt: + return + + pid = txt[txt.rfind(' ') + 1:] + if popen(f'tasklist | findstr {pid}').read().lower().startswith('chrome.exe'): + popen(f'taskkill /pid {pid} /F') def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict],