mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
优化启动浏览器和获取驱动逻辑,未完成
This commit is contained in:
parent
fa847ffb9b
commit
6482dfec51
@ -317,3 +317,25 @@ def unzip(zip_path: str, to_path: str) -> Union[list, None]:
|
|||||||
|
|
||||||
with ZipFile(zip_path, 'r') as f:
|
with ZipFile(zip_path, 'r') as f:
|
||||||
return [f.extract(f.namelist()[0], path=to_path)]
|
return [f.extract(f.namelist()[0], path=to_path)]
|
||||||
|
|
||||||
|
|
||||||
|
def get_exe_path_from_port(port: Union[str, int]) -> Union[str, None]:
|
||||||
|
"""获取端口号第一条进程的可执行文件路径 \n
|
||||||
|
:param port: 端口号
|
||||||
|
:return: 可执行文件的绝对路径
|
||||||
|
"""
|
||||||
|
from os import popen
|
||||||
|
from time import perf_counter
|
||||||
|
process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0]
|
||||||
|
t = perf_counter()
|
||||||
|
|
||||||
|
while not process and perf_counter() - t < 10:
|
||||||
|
process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0]
|
||||||
|
|
||||||
|
processid = process[process.rfind(' ') + 1:]
|
||||||
|
|
||||||
|
if not processid:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
file_lst = popen(f'wmic process where processid={processid} get executablepath').read().split('\n')
|
||||||
|
return file_lst[2].strip() if len(file_lst) > 2 else None
|
||||||
|
@ -35,6 +35,7 @@ class Drission(object):
|
|||||||
"""
|
"""
|
||||||
self._session = None
|
self._session = None
|
||||||
self._driver = None
|
self._driver = None
|
||||||
|
self._debugger = None
|
||||||
self._proxy = proxy
|
self._proxy = proxy
|
||||||
|
|
||||||
om = OptionsManager(ini_path) if session_or_options is None or driver_or_options is None else None
|
om = OptionsManager(ini_path) if session_or_options is None or driver_or_options is None else None
|
||||||
@ -94,41 +95,61 @@ class Drission(object):
|
|||||||
driver_path = self._driver_options.get('driver_path', None) or 'chromedriver'
|
driver_path = self._driver_options.get('driver_path', None) or 'chromedriver'
|
||||||
chrome_path = self._driver_options.get('binary_location', None) or 'chrome.exe'
|
chrome_path = self._driver_options.get('binary_location', None) or 'chrome.exe'
|
||||||
|
|
||||||
|
# -----------若指定debug端口且该端口未在使用中,则先启动浏览器进程-----------
|
||||||
if options.debugger_address and _check_port(options.debugger_address) is False:
|
if options.debugger_address and _check_port(options.debugger_address) is False:
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
port = options.debugger_address[options.debugger_address.rfind(':') + 1:]
|
port = options.debugger_address[options.debugger_address.rfind(':') + 1:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False)
|
self._debugger = Popen(f'{chrome_path} --remote-debugging-port={port}', shell=False)
|
||||||
|
|
||||||
|
if chrome_path == 'chrome.exe':
|
||||||
|
from common import get_exe_path_from_port
|
||||||
|
chrome_path = get_exe_path_from_port(port)
|
||||||
|
|
||||||
|
# 启动不了进程,主动找浏览器执行文件启动
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
from DrissionPage.easy_set import _get_chrome_path
|
from DrissionPage.easy_set import _get_chrome_path
|
||||||
|
|
||||||
chrome_path = _get_chrome_path(show_msg=False)
|
chrome_path = _get_chrome_path(show_msg=False)
|
||||||
|
|
||||||
if not chrome_path:
|
if not chrome_path:
|
||||||
raise FileNotFoundError('无法找到chrome.exe路径,请手动配置。')
|
raise FileNotFoundError('无法找到chrome.exe路径,请手动配置。')
|
||||||
|
|
||||||
Popen(f'"{chrome_path}" --remote-debugging-port={port}', shell=False)
|
self._debugger = Popen(f'"{chrome_path}" --remote-debugging-port={port}', shell=False)
|
||||||
options.binary_location = chrome_path
|
|
||||||
|
|
||||||
|
# -----------创建WebDriver对象-----------
|
||||||
try:
|
try:
|
||||||
self._driver = webdriver.Chrome(driver_path, options=options)
|
self._driver = webdriver.Chrome(driver_path, options=options)
|
||||||
|
|
||||||
|
# 若版本不对,获取对应chromedriver再试
|
||||||
except (WebDriverException, SessionNotCreatedException):
|
except (WebDriverException, SessionNotCreatedException):
|
||||||
from .easy_set import get_match_driver
|
from .easy_set import get_match_driver
|
||||||
|
|
||||||
print('自动下载chromedriver...')
|
|
||||||
chrome_path = None if chrome_path == 'chrome.exe' else chrome_path
|
chrome_path = None if chrome_path == 'chrome.exe' else chrome_path
|
||||||
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False)
|
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False)
|
||||||
|
|
||||||
if driver_path:
|
if driver_path:
|
||||||
try:
|
try:
|
||||||
self._driver = webdriver.Chrome(driver_path, options=options)
|
self._driver = webdriver.Chrome(driver_path, options=options)
|
||||||
print('下载完成。')
|
|
||||||
except:
|
except:
|
||||||
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
# 当找不到driver且chrome_path为None时,说明安装的版本过高,改在系统路径中查找
|
||||||
|
elif chrome_path is None and driver_path is None:
|
||||||
|
from DrissionPage.easy_set import _get_chrome_path
|
||||||
|
chrome_path = _get_chrome_path(show_msg=False, from_ini=False, from_regedit=False)
|
||||||
|
driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=False)
|
||||||
|
|
||||||
|
if driver_path:
|
||||||
|
options.binary_location = chrome_path
|
||||||
|
try:
|
||||||
|
self._driver = webdriver.Chrome(driver_path, options=options)
|
||||||
|
except:
|
||||||
|
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
||||||
|
exit(0)
|
||||||
else:
|
else:
|
||||||
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
print('无法启动,请检查chromedriver版本与Chrome是否匹配,并手动设置。')
|
||||||
exit(0)
|
exit(0)
|
||||||
@ -145,6 +166,11 @@ class Drission(object):
|
|||||||
|
|
||||||
return self._driver
|
return self._driver
|
||||||
|
|
||||||
|
@property
|
||||||
|
def debugger_progress(self):
|
||||||
|
"""调试浏览器进程"""
|
||||||
|
return self._debugger
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def driver_options(self) -> dict:
|
def driver_options(self) -> dict:
|
||||||
"""返回driver配置信息"""
|
"""返回driver配置信息"""
|
||||||
|
@ -242,13 +242,17 @@ def get_match_driver(ini_path: Union[str, None] = 'default',
|
|||||||
return driver_path
|
return driver_path
|
||||||
|
|
||||||
|
|
||||||
def _get_chrome_path(ini_path: str = None, show_msg: bool = True) -> Union[str, None]:
|
def _get_chrome_path(ini_path: str = None,
|
||||||
|
show_msg: bool = True,
|
||||||
|
from_ini: bool = True,
|
||||||
|
from_regedit: bool = True,
|
||||||
|
from_system_path: bool = True, ) -> Union[str, None]:
|
||||||
"""从ini文件或系统变量中获取chrome.exe的路径 \n
|
"""从ini文件或系统变量中获取chrome.exe的路径 \n
|
||||||
:param ini_path: ini文件路径
|
:param ini_path: ini文件路径
|
||||||
:return: chrome.exe路径
|
:return: chrome.exe路径
|
||||||
"""
|
"""
|
||||||
# -----------从ini文件中获取--------------
|
# -----------从ini文件中获取--------------
|
||||||
if ini_path:
|
if ini_path and from_ini:
|
||||||
try:
|
try:
|
||||||
path = OptionsManager(ini_path).chrome_options['binary_location']
|
path = OptionsManager(ini_path).chrome_options['binary_location']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -261,6 +265,7 @@ def _get_chrome_path(ini_path: str = None, show_msg: bool = True) -> Union[str,
|
|||||||
return str(path)
|
return str(path)
|
||||||
|
|
||||||
# -----------从注册表中获取--------------
|
# -----------从注册表中获取--------------
|
||||||
|
if from_regedit:
|
||||||
import winreg
|
import winreg
|
||||||
try:
|
try:
|
||||||
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
|
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
|
||||||
@ -277,7 +282,8 @@ def _get_chrome_path(ini_path: str = None, show_msg: bool = True) -> Union[str,
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# -----------从系统路径中获取--------------
|
# -----------从系统变量中获取--------------
|
||||||
|
if from_system_path:
|
||||||
paths = popen('set path').read().lower()
|
paths = popen('set path').read().lower()
|
||||||
r = RE_SEARCH(r'[^;]*chrome[^;]*', paths)
|
r = RE_SEARCH(r'[^;]*chrome[^;]*', paths)
|
||||||
|
|
||||||
@ -286,7 +292,7 @@ def _get_chrome_path(ini_path: str = None, show_msg: bool = True) -> Union[str,
|
|||||||
|
|
||||||
if path.exists():
|
if path.exists():
|
||||||
if show_msg:
|
if show_msg:
|
||||||
print('系统中', end='')
|
print('系统变量中', end='')
|
||||||
return str(path)
|
return str(path)
|
||||||
|
|
||||||
paths = paths.split(';')
|
paths = paths.split(';')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user