diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index e96c702..8ea4e13 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -5,7 +5,6 @@ """ from json import load, dump from pathlib import Path -from platform import system from subprocess import Popen from tempfile import gettempdir from time import perf_counter, sleep @@ -14,7 +13,7 @@ from requests import get as requests_get from DrissionPage.configs.chromium_options import ChromiumOptions from DrissionPage.errors import BrowserConnectError -from .tools import port_is_using, get_exe_from_port +from .tools import port_is_using def connect_browser(option): @@ -22,7 +21,6 @@ def connect_browser(option): :param option: DriverOptions对象 :return: chrome 路径和进程对象组成的元组 """ - system_type = system().lower() debugger_address = option.debugger_address chrome_path = option.browser_path @@ -33,9 +31,8 @@ def connect_browser(option): return None, None if port_is_using(ip, port): - chrome_path = get_exe_from_port(port) if chrome_path == 'chrome' and system_type == 'windows' else chrome_path test_connect(ip, port) - return chrome_path, None + return None, None args = get_launch_args(option) set_prefs(option) @@ -43,8 +40,6 @@ def connect_browser(option): # ----------创建浏览器进程---------- try: debugger = _run_browser(port, chrome_path, args) - if chrome_path == 'chrome' and system_type == 'windows': - chrome_path = get_exe_from_port(port) # 传入的路径找不到,主动在ini文件、注册表、系统变量中找 except FileNotFoundError: @@ -173,18 +168,6 @@ def _run_browser(port, path: str, args) -> Popen: arguments.extend(args) return Popen(arguments, shell=False) - # end_time = perf_counter() + 10 - # while perf_counter() < end_time: - # try: - # tabs = requests_get(f'http://127.0.0.1:{port}/json', timeout=2).json() - # for tab in tabs: - # if tab['type'] == 'page': - # return debugger - # except Exception: - # sleep(.2) - # - # raise BrowserConnectError - def _make_leave_in_dict(target_dict: dict, src: list, num: int, end: int) -> None: """把prefs中a.b.c形式的属性转为a['b']['c']形式 diff --git a/DrissionPage/commons/tools.py b/DrissionPage/commons/tools.py index 4b1b9cc..c3e942d 100644 --- a/DrissionPage/commons/tools.py +++ b/DrissionPage/commons/tools.py @@ -45,7 +45,7 @@ def get_pid_from_port(port): return process.split(' ')[-1] or None - except AttributeError: + except Exception: return None diff --git a/DrissionPage/mixpage/drission.py b/DrissionPage/mixpage/drission.py index b977cd8..e745220 100644 --- a/DrissionPage/mixpage/drission.py +++ b/DrissionPage/mixpage/drission.py @@ -14,7 +14,7 @@ from selenium.webdriver.chrome.options import Options from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from tldextract import extract -from DrissionPage.commons.tools import get_pid_from_port +from DrissionPage.commons.tools import get_pid_from_port, get_exe_from_port from DrissionPage.commons.browser import connect_browser from DrissionPage.commons.web import cookies_to_tuple from DrissionPage.configs.session_options import SessionOptions, session_options_to_dict @@ -106,7 +106,15 @@ class Drission(object): # -----------若指定debug端口且该端口未在使用中,则先启动浏览器进程----------- if self.driver_options.debugger_address: # 启动浏览器进程,同时返回该进程使用的 chrome.exe 路径 - chrome_path, self._debugger = connect_browser(self.driver_options) + cp, self._debugger = connect_browser(self.driver_options) + + if cp in (None, 'chrome'): + system_type = system().lower() + ip, port = self.driver_options.debugger_address.split(':') + if ip not in ('127.0.0.1', 'localhost'): + chrome_path = None + elif chrome_path == 'chrome' and system_type == 'windows': + chrome_path = get_exe_from_port(port) # -----------创建WebDriver对象----------- self._driver = create_driver(chrome_path, driver_path, self.driver_options) diff --git a/setup.py b/setup.py index 435594f..fbdb367 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.2.10", + version="3.2.12", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",