From fa847ffb9b93f1e1f88acf46e9160371f4cc00f7 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 4 Mar 2021 00:49:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=90=AF=E5=8A=A8chrome?= =?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/drission.py | 25 ++++++++++++++++++++----- DrissionPage/easy_set.py | 6 +++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 3c49500..0006a66 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -96,9 +96,21 @@ class Drission(object): if options.debugger_address and _check_port(options.debugger_address) is False: from subprocess import Popen - port = options.debugger_address.split(':')[-1] + port = options.debugger_address[options.debugger_address.rfind(':') + 1:] - Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False) + try: + Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False) + + except FileNotFoundError: + from DrissionPage.easy_set import _get_chrome_path + + chrome_path = _get_chrome_path(show_msg=False) + + if not chrome_path: + raise FileNotFoundError('无法找到chrome.exe路径,请手动配置。') + + Popen(f'"{chrome_path}" --remote-debugging-port={port}', shell=False) + options.binary_location = chrome_path try: self._driver = webdriver.Chrome(driver_path, options=options) @@ -106,9 +118,9 @@ class Drission(object): except (WebDriverException, SessionNotCreatedException): from .easy_set import get_match_driver - chrome_path = self._driver_options.get('binary_location', None) or None print('自动下载chromedriver...') - driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False) + chrome_path = None if chrome_path == 'chrome.exe' else chrome_path + driver_path = get_match_driver(chrome_path=chrome_path, check_version=False) if driver_path: try: @@ -326,7 +338,7 @@ class Drission(object): def _check_port(debugger_address: str) -> Union[bool, None]: - """检查端口是否可用 \n + """检查端口是否被占用 \n :param debugger_address: 浏览器地址及端口 :return: bool """ @@ -345,3 +357,6 @@ def _check_port(debugger_address: str) -> Union[bool, None]: return True except socket.error: return False + finally: + if s: + s.close() diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index 2d67807..d3f0330 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -208,11 +208,11 @@ def get_match_driver(ini_path: Union[str, None] = 'default', chrome_path = chrome_path or _get_chrome_path(ini_path, show_msg) chrome_path = Path(chrome_path).absolute() if chrome_path else None if show_msg: - print('chrome.exe路径', chrome_path, '\n') + print('chrome.exe路径', chrome_path) ver = _get_chrome_version(chrome_path) if show_msg: - print('version', ver, '\n') + print('version', ver) zip_path = _download_driver(ver, save_path, show_msg=show_msg) @@ -225,7 +225,7 @@ def get_match_driver(ini_path: Union[str, None] = 'default', driver_path = None if show_msg: - print('\n解压路径', driver_path, '\n') + print('解压路径', driver_path) if driver_path: Path(zip_path).unlink()