2.7.1修复使用Anaconda中关闭driver时的小问题

This commit is contained in:
g1879 2022-06-23 11:30:50 +08:00
parent caca993c6a
commit ee61f1d8bd
4 changed files with 17 additions and 13 deletions

View File

@ -370,20 +370,24 @@ def get_pid_from_port(port: Union[str, int]) -> Union[str, None]:
:param port: 端口号 :param port: 端口号
:return: 进程id :return: 进程id
""" """
from os import popen
from platform import system from platform import system
if system().lower() != 'windows' or port is None:
return None
from os import popen
from time import perf_counter from time import perf_counter
if system().lower() != 'windows' or port is None: try: # 避免Anaconda中可能产生的报错
return
process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0]
t = perf_counter()
while not process and perf_counter() - t < 5:
process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0] process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0]
return process.split(' ')[-1] or None t = perf_counter()
while not process and perf_counter() - t < 5:
process = popen(f'netstat -ano |findstr {port}').read().split('\n')[0]
return process.split(' ')[-1] or None
except AttributeError:
return None
def get_usable_path(path: Union[str, Path]) -> Path: def get_usable_path(path: Union[str, Path]) -> Path:

View File

@ -504,7 +504,7 @@ def _create_driver(chrome_path: str, driver_path: str, options: Options) -> WebD
def _get_chrome_hwnds_from_pid(pid) -> list: def _get_chrome_hwnds_from_pid(pid) -> list:
# 通过PID查询句柄ID """通过PID查询句柄ID"""
try: try:
from win32gui import IsWindow, GetWindowText, EnumWindows from win32gui import IsWindow, GetWindowText, EnumWindows
from win32process import GetWindowThreadProcessId from win32process import GetWindowThreadProcessId
@ -524,7 +524,7 @@ def _get_chrome_hwnds_from_pid(pid) -> list:
def _kill_progress(pid: str = None, port: int = None) -> bool: def _kill_progress(pid: str = None, port: int = None) -> bool:
"""获取端口号第一条进程的pid \n """关闭浏览器进程 \n
:param pid: 进程id :param pid: 进程id
:param port: 端口号如没有进程id从端口号获取 :param port: 端口号如没有进程id从端口号获取
:return: 是否成功 :return: 是否成功

View File

@ -1,4 +1,4 @@
# v2.7.0 # v2.7.1
- DriverPage - DriverPage

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="2.7.0", version="2.7.1",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.", description="A module that integrates selenium and requests session, encapsulates common page operations.",