3.2.10增加可指定使用系统浏览器默认用户文件夹功能

This commit is contained in:
g1879 2023-03-04 10:45:27 +08:00
parent 55e6d2dbd5
commit d3b14b8dbc
7 changed files with 44 additions and 5 deletions

View File

@ -71,13 +71,13 @@ def get_launch_args(opt):
for i in opt.arguments: for i in opt.arguments:
if i.startswith(('--load-extension=', '--remote-debugging-port=')): if i.startswith(('--load-extension=', '--remote-debugging-port=')):
continue continue
elif i.startswith('--user-data-dir'): elif i.startswith('--user-data-dir') and not opt.system_user_path:
p = Path(i[16:]).absolute() p = Path(i[16:]).absolute()
result.add(f'--user-data-dir={p}') result.add(f'--user-data-dir={p}')
has_user_path = True has_user_path = True
result.add(i) result.add(i)
if not has_user_path: if not has_user_path and not opt.system_user_path:
port = opt.debugger_address.split(':')[-1] if opt.debugger_address else '0' port = opt.debugger_address.split(':')[-1] if opt.debugger_address else '0'
path = Path(gettempdir()) / 'DrissionPage' / f'userData_{port}' path = Path(gettempdir()) / 'DrissionPage' / f'userData_{port}'
path.mkdir(parents=True, exist_ok=True) path.mkdir(parents=True, exist_ok=True)
@ -146,7 +146,7 @@ def test_connect(ip, port):
:param port: 浏览器端口 :param port: 浏览器端口
:return: None :return: None
""" """
end_time = perf_counter() + 10 end_time = perf_counter() + 6
while perf_counter() < end_time: while perf_counter() < end_time:
try: try:
tabs = requests_get(f'http://{ip}:{port}/json', timeout=3).json() tabs = requests_get(f'http://{ip}:{port}/json', timeout=3).json()
@ -157,7 +157,8 @@ def test_connect(ip, port):
sleep(.2) sleep(.2)
if ip in ('127.0.0.1', 'localhost'): if ip in ('127.0.0.1', 'localhost'):
raise BrowserConnectError(f'{port}端口不是Chromium内核浏览器或该浏览器未允许控制。') raise BrowserConnectError(f'\n连接浏览器失败,可能原因:\n1、{port}端口不是Chromium内核浏览器\n'
f'2、该浏览器未允许控制\n3、和已打开的浏览器冲突请关闭')
raise BrowserConnectError(f'{ip}:{port}浏览器无法链接。') raise BrowserConnectError(f'{ip}:{port}浏览器无法链接。')

View File

@ -34,6 +34,7 @@ class ChromiumOptions(object):
self._debugger_address = options.get('debugger_address', None) self._debugger_address = options.get('debugger_address', None)
self._page_load_strategy = options.get('page_load_strategy', 'normal') self._page_load_strategy = options.get('page_load_strategy', 'normal')
self._proxy = om.proxies.get('http', None) self._proxy = om.proxies.get('http', None)
self._system_user_path = options.get('system_user_path', False)
user_path = user = False user_path = user = False
for arg in self._arguments: for arg in self._arguments:
@ -69,6 +70,7 @@ class ChromiumOptions(object):
self._page_load_strategy = 'normal' self._page_load_strategy = 'normal'
self._proxy = None self._proxy = None
self._auto_port = False self._auto_port = False
self._system_user_path = False
@property @property
def download_path(self): def download_path(self):
@ -130,6 +132,11 @@ class ChromiumOptions(object):
"""返回用户首选项配置""" """返回用户首选项配置"""
return self._prefs return self._prefs
@property
def system_user_path(self):
"""返回是否使用系统安装的浏览器所使用的用户数据文件夹"""
return self._system_user_path
def set_argument(self, arg, value=None): def set_argument(self, arg, value=None):
"""设置浏览器配置的argument属性 """设置浏览器配置的argument属性
:param arg: 属性名 :param arg: 属性名
@ -322,6 +329,14 @@ class ChromiumOptions(object):
return self return self
def use_system_user_path(self, on_off=True):
"""设置是否使用系统安装的浏览器默认用户文件夹
:param on_off: 开或关
:return: 当前对象
"""
self._system_user_path = on_off
return self
def auto_port(self, on_off=True): def auto_port(self, on_off=True):
"""自动获取可用端口 """自动获取可用端口
:param on_off: 是否开启自动获取端口号 :param on_off: 是否开启自动获取端口号

View File

@ -24,6 +24,7 @@ class ChromiumOptions(object):
self._prefs: dict = ... self._prefs: dict = ...
self._prefs_to_del: list = ... self._prefs_to_del: list = ...
self._auto_port: bool = ... self._auto_port: bool = ...
self._system_user_path: bool = ...
@property @property
def download_path(self) -> str: ... def download_path(self) -> str: ...
@ -61,6 +62,9 @@ class ChromiumOptions(object):
@property @property
def preferences(self) -> dict: ... def preferences(self) -> dict: ...
@property
def system_user_path(self) -> bool: ...
def set_argument(self, arg: str, value: Union[str, None, bool] = None) -> ChromiumOptions: ... def set_argument(self, arg: str, value: Union[str, None, bool] = None) -> ChromiumOptions: ...
def remove_argument(self, value: str) -> ChromiumOptions: ... def remove_argument(self, value: str) -> ChromiumOptions: ...
@ -98,6 +102,8 @@ class ChromiumOptions(object):
debugger_address: str = None, download_path: Union[str, Path] = None, debugger_address: str = None, download_path: Union[str, Path] = None,
user_data_path: Union[str, Path] = None, cache_path: Union[str, Path] = None) -> ChromiumOptions: ... user_data_path: Union[str, Path] = None, cache_path: Union[str, Path] = None) -> ChromiumOptions: ...
def use_system_user_path(self, on_off: bool = True) -> ChromiumOptions: ...
def auto_port(self, on_off: bool = True) -> ChromiumOptions: ... def auto_port(self, on_off: bool = True) -> ChromiumOptions: ...
def save(self, path: Union[str, Path] = None) -> str: ... def save(self, path: Union[str, Path] = None) -> str: ...

View File

@ -11,6 +11,7 @@ experimental_options = {'prefs': {'profile.default_content_settings.popups': 0,
page_load_strategy = normal page_load_strategy = normal
user = Default user = Default
auto_port = False auto_port = False
system_user_path = False
[session_options] [session_options]
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'connection': 'keep-alive', 'accept-charset': 'GB2312,utf-8;q=0.7,*;q=0.7'} headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'connection': 'keep-alive', 'accept-charset': 'GB2312,utf-8;q=0.7,*;q=0.7'}

View File

@ -119,6 +119,19 @@ def use_auto_port(on_off=True, ini_path=None):
om.save() om.save()
def use_system_user_path(on_off=True, ini_path=None):
"""设置是否使用系统安装的浏览器默认用户文件夹
:param on_off: 开或关
:param ini_path: 要修改的ini文件路径
:return: 当前对象
"""
if not isinstance(on_off, bool):
raise TypeError('on_off参数只能输入bool值。')
om = OptionsManager(ini_path)
om.set_item('chrome_options', 'system_user_path', on_off)
om.save()
def set_argument(arg, value=None, ini_path=None): def set_argument(arg, value=None, ini_path=None):
"""设置浏览器配置argument属性 """设置浏览器配置argument属性
:param arg: 属性名 :param arg: 属性名

View File

@ -31,6 +31,9 @@ def set_paths(driver_path: Union[str, Path] = None,
def use_auto_port(on_off: bool = True, ini_path: Union[str, Path] = None) -> None: ... def use_auto_port(on_off: bool = True, ini_path: Union[str, Path] = None) -> None: ...
def use_system_user_path(on_off: bool = True, ini_path: Union[str, Path] = None) -> None: ...
def set_argument(arg: str, value: Union[bool, str] = None, ini_path: Union[str, Path] = None) -> None: ... def set_argument(arg: str, value: Union[bool, str] = None, ini_path: Union[str, Path] = None) -> None: ...

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="3.2.9", version="3.2.10",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.", description="Python based web automation tool. It can control the browser and send and receive data packets.",