优化connect_browser()逻辑

This commit is contained in:
g1879 2023-03-06 18:22:37 +08:00
parent 1550e8d673
commit 7c43573fad
4 changed files with 14 additions and 23 deletions

View File

@ -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']形式

View File

@ -45,7 +45,7 @@ def get_pid_from_port(port):
return process.split(' ')[-1] or None
except AttributeError:
except Exception:
return None

View File

@ -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)

View File

@ -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.",