diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index 741e6be..e96c702 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -71,13 +71,13 @@ def get_launch_args(opt): for i in opt.arguments: if i.startswith(('--load-extension=', '--remote-debugging-port=')): continue - elif i.startswith('--user-data-dir'): + elif i.startswith('--user-data-dir') and not opt.system_user_path: p = Path(i[16:]).absolute() result.add(f'--user-data-dir={p}') has_user_path = True 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' path = Path(gettempdir()) / 'DrissionPage' / f'userData_{port}' path.mkdir(parents=True, exist_ok=True) @@ -146,7 +146,7 @@ def test_connect(ip, port): :param port: 浏览器端口 :return: None """ - end_time = perf_counter() + 10 + end_time = perf_counter() + 6 while perf_counter() < end_time: try: tabs = requests_get(f'http://{ip}:{port}/json', timeout=3).json() @@ -157,7 +157,8 @@ def test_connect(ip, port): sleep(.2) 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}浏览器无法链接。') diff --git a/DrissionPage/configs/chromium_options.py b/DrissionPage/configs/chromium_options.py index 996f1d8..7c941f8 100644 --- a/DrissionPage/configs/chromium_options.py +++ b/DrissionPage/configs/chromium_options.py @@ -34,6 +34,7 @@ class ChromiumOptions(object): self._debugger_address = options.get('debugger_address', None) self._page_load_strategy = options.get('page_load_strategy', 'normal') self._proxy = om.proxies.get('http', None) + self._system_user_path = options.get('system_user_path', False) user_path = user = False for arg in self._arguments: @@ -69,6 +70,7 @@ class ChromiumOptions(object): self._page_load_strategy = 'normal' self._proxy = None self._auto_port = False + self._system_user_path = False @property def download_path(self): @@ -130,6 +132,11 @@ class ChromiumOptions(object): """返回用户首选项配置""" return self._prefs + @property + def system_user_path(self): + """返回是否使用系统安装的浏览器所使用的用户数据文件夹""" + return self._system_user_path + def set_argument(self, arg, value=None): """设置浏览器配置的argument属性 :param arg: 属性名 @@ -322,6 +329,14 @@ class ChromiumOptions(object): 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): """自动获取可用端口 :param on_off: 是否开启自动获取端口号 diff --git a/DrissionPage/configs/chromium_options.pyi b/DrissionPage/configs/chromium_options.pyi index 16e4f76..68937fc 100644 --- a/DrissionPage/configs/chromium_options.pyi +++ b/DrissionPage/configs/chromium_options.pyi @@ -24,6 +24,7 @@ class ChromiumOptions(object): self._prefs: dict = ... self._prefs_to_del: list = ... self._auto_port: bool = ... + self._system_user_path: bool = ... @property def download_path(self) -> str: ... @@ -61,6 +62,9 @@ class ChromiumOptions(object): @property 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 remove_argument(self, value: str) -> ChromiumOptions: ... @@ -98,6 +102,8 @@ class ChromiumOptions(object): debugger_address: str = None, download_path: Union[str, Path] = None, 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 save(self, path: Union[str, Path] = None) -> str: ... diff --git a/DrissionPage/configs/configs.ini b/DrissionPage/configs/configs.ini index 3c5e32f..4f1ed10 100644 --- a/DrissionPage/configs/configs.ini +++ b/DrissionPage/configs/configs.ini @@ -11,6 +11,7 @@ experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, page_load_strategy = normal user = Default auto_port = False +system_user_path = False [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'} diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index d62a03c..b949e4a 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -119,6 +119,19 @@ def use_auto_port(on_off=True, ini_path=None): 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): """设置浏览器配置argument属性 :param arg: 属性名 diff --git a/DrissionPage/easy_set.pyi b/DrissionPage/easy_set.pyi index fad8e17..7ea52ca 100644 --- a/DrissionPage/easy_set.pyi +++ b/DrissionPage/easy_set.pyi @@ -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_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: ... diff --git a/setup.py b/setup.py index 76badb1..435594f 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.9", + version="3.2.10", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",