From 79859193651ad4fe0769250efac58ecb215c67a4 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 7 Dec 2020 13:58:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAdriver=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=8C=87=E5=AE=9A=E4=BA=86=E6=9C=AC=E5=9C=B0=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=B5=8F=E8=A7=88=E5=99=A8=EF=BC=8C=E5=8F=AF=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8E=A5=E5=85=A5=E6=88=96=E5=88=9B=E5=BB=BA=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/drission.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 8be5cc6..e305a54 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -79,7 +79,9 @@ class Drission(object): @property def driver(self) -> WebDriver: - """返回WebDriver对象,如未初始化则按配置信息创建""" + """返回WebDriver对象,如未初始化则按配置信息创建。 \n + 如设置了本地调试浏览器,可自动接入或打开浏览器进程。 + """ if self._driver is None: if isinstance(self._driver_options, dict): options = _dict_to_chrome_options(self._driver_options) @@ -92,7 +94,14 @@ class Drission(object): driver_path = self._driver_options.get('driver_path', None) or 'chromedriver' try: + if options.debugger_address and _check_port(options.debugger_address) is False: + from subprocess import Popen + port = options.debugger_address.split(':')[-1] + chrome_path = self._driver_options.get('binary_location', None) or 'chrome.exe' + Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False) + self._driver = webdriver.Chrome(driver_path, options=options) + except SessionNotCreatedException: print('Chrome版本与chromedriver版本不匹配,可执行easy_set.get_match_driver()自动下载匹配的版本。') exit(0) @@ -279,3 +288,25 @@ class Drission(object): self.close() except ImportError: pass + + +def _check_port(debugger_address: str) -> Union[bool, None]: + """检查端口是否可用 \n + :param debugger_address: 浏览器地址及端口 + :return: bool + """ + import socket + + ip, port = debugger_address.split(':') + + if ip not in ('127.0.0.1', 'localhost'): + return + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + try: + s.connect((ip, int(port))) + s.shutdown(2) + return True + except socket.error: + return False