From acfd774d1f712e3a0b1bc2bc32ac32f0b986f910 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 26 Nov 2023 23:15:06 +0800 Subject: [PATCH] =?UTF-8?q?4.0.0b13=EF=BC=88=E8=AF=A6=EF=BC=89=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=B1=BB=E5=92=8Cini=E6=96=87=E4=BB=B6=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E8=BF=9E=E6=8E=A5=E9=87=8D=E8=AF=95=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=EF=BC=9B=20ini=E6=96=87=E4=BB=B6chrome=5Foptions=E6=94=B9?= =?UTF-8?q?=E4=B8=BAchromium=5Foptions=20@&=E6=94=B9=E5=9B=9E@@=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=92=8C=E4=BF=AE=E5=A4=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=20WebPage=E7=9A=84driver=5Foptions?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=B9=E4=B8=BAchromium=5Foptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_commons/browser.py | 2 +- DrissionPage/_commons/locator.py | 112 ++++++--------------- DrissionPage/_configs/chromium_options.py | 42 +++++++- DrissionPage/_configs/chromium_options.pyi | 10 ++ DrissionPage/_configs/configs.ini | 5 +- DrissionPage/_configs/session_options.py | 70 +++++++++---- DrissionPage/_configs/session_options.pyi | 10 ++ DrissionPage/_pages/chromium_page.py | 47 ++++----- DrissionPage/_pages/chromium_page.pyi | 2 +- DrissionPage/_pages/chromium_tab.py | 2 +- DrissionPage/_pages/session_page.py | 2 + DrissionPage/_pages/web_page.py | 18 ++-- DrissionPage/_pages/web_page.pyi | 4 +- setup.py | 2 +- 15 files changed, 184 insertions(+), 146 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index c4bce69..6be91ef 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.0b12' +__version__ = '4.0.0b13' diff --git a/DrissionPage/_commons/browser.py b/DrissionPage/_commons/browser.py index b8c3440..b263674 100644 --- a/DrissionPage/_commons/browser.py +++ b/DrissionPage/_commons/browser.py @@ -293,7 +293,7 @@ def get_chrome_path(ini_path=None, show_msg=True, from_ini=True, # -----------从ini文件中获取-------------- if ini_path and from_ini: try: - path = OptionsManager(ini_path).chrome_options['browser_path'] + path = OptionsManager(ini_path).chromium_options['browser_path'] except KeyError: path = None else: diff --git a/DrissionPage/_commons/locator.py b/DrissionPage/_commons/locator.py index 608b796..58d6222 100644 --- a/DrissionPage/_commons/locator.py +++ b/DrissionPage/_commons/locator.py @@ -68,21 +68,9 @@ def str_to_xpath_loc(loc): # ------------------------------------------------------------------ # 多属性查找 - if loc.startswith('@!') and loc != '@!': - r = split(r'(@!|@&|@\|)', loc) - if '@&' in r and '@|' in r: - raise ValueError('@&和@|不能同时出现在一个定位语句中。') - elif '@&' in r: - loc_str = _make_multi_xpath_str('*', loc)[1] - else: # @| - loc_str = _make_multi_xpath_str('*', loc, False)[1] - - elif loc.startswith('@&') and loc != '@&': + if loc.startswith(('@@', '@|', '@!')) and loc not in ('@@', '@|', '@!'): loc_str = _make_multi_xpath_str('*', loc)[1] - elif loc.startswith('@|') and loc != '@|': - loc_str = _make_multi_xpath_str('*', loc, False)[1] - # 单属性查找 elif loc.startswith('@') and loc != '@': loc_str = _make_single_xpath_str('*', loc)[1] @@ -92,24 +80,18 @@ def str_to_xpath_loc(loc): at_ind = loc.find('@') if at_ind == -1: loc_str = f'//*[name()="{loc[4:]}"]' + elif loc[at_ind:].startswith(('@@', '@|', '@!')): + loc_str = _make_multi_xpath_str(loc[4:at_ind], loc[at_ind:])[1] else: - if loc[at_ind:].startswith(('@&', '@!')): - loc_str = _make_multi_xpath_str(loc[4:at_ind], loc[at_ind:])[1] - elif loc[at_ind:].startswith('@|'): - loc_str = _make_multi_xpath_str(loc[4:at_ind], loc[at_ind:], False)[1] - else: - loc_str = _make_single_xpath_str(loc[4:at_ind], loc[at_ind:])[1] + loc_str = _make_single_xpath_str(loc[4:at_ind], loc[at_ind:])[1] # 根据文本查找 elif loc.startswith('text='): loc_str = f'//*[text()={_make_search_str(loc[5:])}]' - elif loc.startswith('text:') and loc != 'text:': loc_str = f'//*/text()[contains(., {_make_search_str(loc[5:])})]/..' - elif loc.startswith('text^') and loc != 'text^': loc_str = f'//*/text()[starts-with(., {_make_search_str(loc[5:])})]/..' - elif loc.startswith('text$') and loc != 'text$': loc_str = f'//*/text()[substring(., string-length(.) - string-length({_make_search_str(loc[5:])}) +1) = ' \ f'{_make_search_str(loc[5:])}]/..' @@ -164,20 +146,8 @@ def str_to_css_loc(loc): # ------------------------------------------------------------------ # 多属性查找 - if loc.startswith('@!') and loc != '@!': - r = split(r'(@!|@&|@\|)', loc) - if '@&' in r and '@|' in r: - raise ValueError('@&和@|不能同时出现在一个定位语句中。') - elif '@&' in r: - loc_by, loc_str = _make_multi_css_str('*', loc) - else: # @| - loc_by, loc_str = _make_multi_css_str('*', loc, False) - - elif loc.startswith('@&') and loc != '@&': - loc_by, loc_str = _make_multi_css_str('*', loc) - - elif loc.startswith('@|') and loc != '@|': - loc_by, loc_str = _make_multi_css_str('*', loc, False) + if loc.startswith(('@@', '@|', '@!')) and loc not in ('@@', '@|', '@!'): + loc_str = _make_multi_css_str('*', loc)[1] # 单属性查找 elif loc.startswith('@') and loc != '@': @@ -188,39 +158,14 @@ def str_to_css_loc(loc): at_ind = loc.find('@') if at_ind == -1: loc_str = loc[4:] + elif loc[at_ind:].startswith(('@@', '@|', '@!')): + loc_by, loc_str = _make_multi_css_str(loc[4:at_ind], loc[at_ind:]) else: - if loc[at_ind:].startswith(('@&', '@!')): - loc_by, loc_str = _make_multi_css_str(loc[4:at_ind], loc[at_ind:]) - elif loc[at_ind:].startswith('@|'): - loc_by, loc_str = _make_multi_css_str(loc[4:at_ind], loc[at_ind:], False) - else: - loc_by, loc_str = _make_single_css_str(loc[4:at_ind], loc[at_ind:]) + loc_by, loc_str = _make_single_css_str(loc[4:at_ind], loc[at_ind:]) # 根据文本查找 - elif loc.startswith('text='): - loc_by = 'xpath' - loc_str = f'//*[text()={_make_search_str(loc[5:])}]' - - elif loc.startswith('text:') and loc != 'text:': - loc_by = 'xpath' - loc_str = f'//*/text()[contains(., {_make_search_str(loc[5:])})]/..' - - elif loc.startswith('text^') and loc != 'text^': - loc_by = 'xpath' - loc_str = f'//*/text()[starts-with(., {_make_search_str(loc[5:])})]/..' - - elif loc.startswith('text$') and loc != 'text$': - loc_by = 'xpath' - loc_str = f'//*/text()[substring(., string-length(.) - string-length({_make_search_str(loc[5:])}) +1) = ' \ - f'{_make_search_str(loc[5:])}]/..' - - # 用xpath查找 - elif loc.startswith(('xpath:', 'xpath=')) and loc not in ('xpath:', 'xpath='): - loc_by = 'xpath' - loc_str = loc[6:] - elif loc.startswith(('x:', 'x=')) and loc not in ('x:', 'x='): - loc_by = 'xpath' - loc_str = loc[2:] + elif loc.startswith(('text=', 'text:', 'text^', 'text$', 'xpath=', 'xpath:', 'x:', 'x=')): + loc_by, loc_str = str_to_xpath_loc(loc) # 用css selector查找 elif loc.startswith(('css:', 'css=')) and loc not in ('css:', 'css='): @@ -230,8 +175,7 @@ def str_to_css_loc(loc): # 根据文本模糊查找 elif loc: - loc_by = 'xpath' - loc_str = f'//*/text()[contains(., {_make_search_str(loc)})]/..' + loc_by, loc_str = str_to_xpath_loc(loc) else: loc_str = '*' @@ -296,17 +240,20 @@ def _make_single_xpath_str(tag: str, text: str) -> tuple: return 'xpath', f'//*[{arg_str}]{txt_str}' if arg_str else f'//*{txt_str}' -def _make_multi_xpath_str(tag: str, text: str, _and: bool = True) -> tuple: +def _make_multi_xpath_str(tag: str, text: str) -> tuple: """生成多属性查找的xpath语句 :param tag: 标签名 :param text: 待处理的字符串 - :param _and: 是否与方式 :return: xpath字符串 """ arg_list = [] - args = split(r'(@!|@&)', text)[1:] if _and else split(r'(@!|@\|)', text)[1:] - if (_and and '@|' in args) or (not _and and '@&' in args): - raise ValueError('@&和@|不能同时出现在一个定位语句中。') + args = split(r'(@!|@@|@\|)', text)[1:] + if '@@' in args and '@|' in args: + raise ValueError('@@和@|不能同时出现在一个定位语句中。') + elif '@@' in args: + _and = True + else: # @| + _and = False for k in range(0, len(args) - 1, 2): r = split(r'([:=$^])', args[k + 1], maxsplit=1) @@ -371,22 +318,25 @@ def _make_search_str(search_str: str) -> str: return search_str -def _make_multi_css_str(tag: str, text: str, _and: bool = True) -> tuple: +def _make_multi_css_str(tag: str, text: str) -> tuple: """生成多属性查找的css selector语句 :param tag: 标签名 :param text: 待处理的字符串 - :param _and: 是否与方式 :return: css selector字符串 """ arg_list = [] - args = split(r'(@!|@&)', text)[1:] if _and else split(r'(@!|@\|)', text)[1:] - if (_and and '@|' in args) or (not _and and '@&' in args): - raise ValueError('@&和@|不能同时出现在一个定位语句中。') + args = split(r'(@!|@@|@\|)', text)[1:] + if '@@' in args and '@|' in args: + raise ValueError('@@和@|不能同时出现在一个定位语句中。') + elif '@@' in args: + _and = True + else: # @| + _and = False - for k in range(0, len(args)-1, 2): - r = split(r'([:=$^])', args[k+1], maxsplit=1) + for k in range(0, len(args) - 1, 2): + r = split(r'([:=$^])', args[k + 1], maxsplit=1) if not r[0] or r[0].startswith(('text()', 'tx()')): - return _make_multi_xpath_str(tag, text, _and) + return _make_multi_xpath_str(tag, text) arg_str = '' len_r = len(r) diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 3f1e962..9ec74b0 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -28,8 +28,8 @@ class ChromiumOptions(object): ini_path = str(ini_path) if ini_path else None om = OptionsManager(ini_path) self.ini_path = om.ini_path - options = om.chrome_options + options = om.chromium_options self._download_path = om.paths.get('download_path', None) or None self._arguments = options.get('arguments', []) self._browser_path = options.get('browser_path', '') @@ -63,6 +63,11 @@ class ChromiumOptions(object): port, path = PortFinder().get_port() self._debugger_address = f'127.0.0.1:{port}' self.set_argument('--user-data-dir', path) + + others = om.others + self._retry_times = others.get('retry_times', 3) + self._retry_interval = others.get('retry_interval', 2) + return self.ini_path = None @@ -79,6 +84,8 @@ class ChromiumOptions(object): self._auto_port = False self._system_user_path = False self._existing_only = False + self._retry_times = 3 + self._retry_interval = 2 @property def download_path(self): @@ -155,6 +162,28 @@ class ChromiumOptions(object): """返回是否只接管现有浏览器方式""" return self._existing_only + @property + def retry_times(self): + """返回连接失败时的重试次数""" + return self._retry_times + + @property + def retry_interval(self): + """返回连接失败时的重试间隔(秒)""" + return self._retry_interval + + def set_retry(self, times=None, interval=None): + """设置连接失败时的重试操作 + :param times: 重试次数 + :param interval: 重试间隔 + :return: 当前对象 + """ + if times is not None: + self._retry_times = times + if interval is not None: + self._retry_interval = interval + return self + def set_argument(self, arg, value=None): """设置浏览器配置的argument属性 :param arg: 属性名 @@ -477,22 +506,25 @@ class ChromiumOptions(object): else: om = OptionsManager(self.ini_path or str(Path(__file__).parent / 'configs.ini')) - # 设置chrome_options + # 设置chromium_options attrs = ('debugger_address', 'browser_path', 'arguments', 'extensions', 'user', 'load_mode', 'auto_port', 'system_user_path', 'existing_only', 'flags') for i in attrs: - om.set_item('chrome_options', i, self.__getattribute__(f'_{i}')) + om.set_item('chromium_options', i, self.__getattribute__(f'_{i}')) # 设置代理 om.set_item('proxies', 'http', self._proxy) om.set_item('proxies', 'https', self._proxy) # 设置路径 - om.set_item('paths', 'download_path', self._download_path) + om.set_item('paths', 'download_path', self._download_path or '') # 设置timeout om.set_item('timeouts', 'implicit', self._timeouts['implicit']) om.set_item('timeouts', 'page_load', self._timeouts['pageLoad']) om.set_item('timeouts', 'script', self._timeouts['script']) + # 设置重试 + om.set_item('others', 'retry_times', self.retry_times) + om.set_item('others', 'retry_interval', self.retry_interval) # 设置prefs - om.set_item('chrome_options', 'prefs', self._prefs) + om.set_item('chromium_options', 'prefs', self._prefs) path = str(path) om.save(path) diff --git a/DrissionPage/_configs/chromium_options.pyi b/DrissionPage/_configs/chromium_options.pyi index 76f8624..9a1b0d4 100644 --- a/DrissionPage/_configs/chromium_options.pyi +++ b/DrissionPage/_configs/chromium_options.pyi @@ -29,6 +29,8 @@ class ChromiumOptions(object): self._system_user_path: bool = ... self._existing_only: bool = ... self._headless: bool = ... + self._retry_times: int = ... + self._retry_interval: float = ... @property def download_path(self) -> str: ... @@ -72,6 +74,14 @@ class ChromiumOptions(object): @property def is_existing_only(self) -> bool: ... + @property + def retry_times(self) -> int: ... + + @property + def retry_interval(self) -> float: ... + + def set_retry(self, times: int = None, interval: float = None) -> ChromiumOptions: ... + def set_argument(self, arg: str, value: Union[str, None, bool] = None) -> ChromiumOptions: ... def remove_argument(self, value: str) -> ChromiumOptions: ... diff --git a/DrissionPage/_configs/configs.ini b/DrissionPage/_configs/configs.ini index a977a3b..72722bd 100644 --- a/DrissionPage/_configs/configs.ini +++ b/DrissionPage/_configs/configs.ini @@ -1,7 +1,7 @@ [paths] download_path = -[chrome_options] +[chromium_options] debugger_address = 127.0.0.1:9222 browser_path = chrome arguments = ['--no-first-run', '--disable-infobars', '--disable-popup-blocking'] @@ -27,3 +27,6 @@ script = 30 http = https = +[others] +retry_times = 3 +retry_interval = 2 diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index c702991..728c6a1 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -37,6 +37,8 @@ class SessionOptions(object): self._stream = None self._trust_env = None self._max_redirects = None + self._retry_times = 3 + self._retry_interval = 2 if read_file is False: return @@ -44,39 +46,43 @@ class SessionOptions(object): ini_path = str(ini_path) if ini_path else None om = OptionsManager(ini_path) self.ini_path = om.ini_path - options_dict = om.session_options - if options_dict.get('headers', None) is not None: - self.set_headers(options_dict['headers']) + options = om.session_options + if options.get('headers', None) is not None: + self.set_headers(options['headers']) - if options_dict.get('cookies', None) is not None: - self.set_cookies(options_dict['cookies']) + if options.get('cookies', None) is not None: + self.set_cookies(options['cookies']) - if options_dict.get('auth', None) is not None: - self._auth = options_dict['auth'] + if options.get('auth', None) is not None: + self._auth = options['auth'] - if options_dict.get('params', None) is not None: - self._params = options_dict['params'] + if options.get('params', None) is not None: + self._params = options['params'] - if options_dict.get('verify', None) is not None: - self._verify = options_dict['verify'] + if options.get('verify', None) is not None: + self._verify = options['verify'] - if options_dict.get('cert', None) is not None: - self._cert = options_dict['cert'] + if options.get('cert', None) is not None: + self._cert = options['cert'] - if options_dict.get('stream', None) is not None: - self._stream = options_dict['stream'] + if options.get('stream', None) is not None: + self._stream = options['stream'] - if options_dict.get('trust_env', None) is not None: - self._trust_env = options_dict['trust_env'] + if options.get('trust_env', None) is not None: + self._trust_env = options['trust_env'] - if options_dict.get('max_redirects', None) is not None: - self._max_redirects = options_dict['max_redirects'] + if options.get('max_redirects', None) is not None: + self._max_redirects = options['max_redirects'] self.set_proxies(om.proxies.get('http', None), om.proxies.get('https', None)) self._timeout = om.timeouts.get('implicit', 10) self._download_path = om.paths.get('download_path', None) or None + others = om.others + self._retry_times = others.get('retry_times', 3) + self._retry_interval = others.get('retry_interval', 2) + # ===========须独立处理的项开始============ @property def download_path(self): @@ -120,6 +126,28 @@ class SessionOptions(object): self._sets('proxies', {'http': http, 'https': https}) return self + @property + def retry_times(self): + """返回连接失败时的重试次数""" + return self._retry_times + + @property + def retry_interval(self): + """返回连接失败时的重试间隔(秒)""" + return self._retry_interval + + def set_retry(self, times=None, interval=None): + """设置连接失败时的重试操作 + :param times: 重试次数 + :param interval: 重试间隔 + :return: 当前对象 + """ + if times is not None: + self._retry_times = times + if interval is not None: + self._retry_interval = interval + return self + # ===========须独立处理的项结束============ @property @@ -350,10 +378,12 @@ class SessionOptions(object): if i not in ('download_path', 'timeout', 'proxies'): om.set_item('session_options', i, options[i]) - om.set_item('paths', 'download_path', self.download_path) + om.set_item('paths', 'download_path', self.download_path or '') om.set_item('timeouts', 'implicit', self.timeout) om.set_item('proxies', 'http', self.proxies.get('http', None)) om.set_item('proxies', 'https', self.proxies.get('https', None)) + om.set_item('others', 'retry_times', self.retry_times) + om.set_item('others', 'retry_interval', self.retry_interval) for i in self._del_set: if i == 'download_path': diff --git a/DrissionPage/_configs/session_options.pyi b/DrissionPage/_configs/session_options.pyi index e4fc4cf..cbd2931 100644 --- a/DrissionPage/_configs/session_options.pyi +++ b/DrissionPage/_configs/session_options.pyi @@ -31,6 +31,8 @@ class SessionOptions(object): self._max_redirects: int = ... self._timeout: float = ... self._del_set: set = ... + self._retry_times: int = ... + self._retry_interval: float = ... @property def download_path(self) -> str: ... @@ -66,6 +68,14 @@ class SessionOptions(object): def set_proxies(self, http: Union[str, None], https: Union[str, None] = None) -> SessionOptions: ... + @property + def retry_times(self) -> int: ... + + @property + def retry_interval(self) -> float: ... + + def set_retry(self, times: int = None, interval: float = None) -> SessionOptions: ... + @property def hooks(self) -> dict: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 54e2be6..677ce88 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -42,53 +42,54 @@ class ChromiumPage(ChromiumBase): :return: 返回浏览器地址 """ if not addr_or_opts: - self._driver_options = ChromiumOptions(addr_or_opts) + self._chromium_options = ChromiumOptions(addr_or_opts) elif isinstance(addr_or_opts, ChromiumOptions): - self._driver_options = addr_or_opts + self._chromium_options = addr_or_opts elif isinstance(addr_or_opts, str): - self._driver_options = ChromiumOptions() - self._driver_options.set_debugger_address(addr_or_opts) + self._chromium_options = ChromiumOptions() + self._chromium_options.set_debugger_address(addr_or_opts) elif isinstance(addr_or_opts, int): - self._driver_options = ChromiumOptions() - self._driver_options.set_local_port(addr_or_opts) + self._chromium_options = ChromiumOptions() + self._chromium_options.set_local_port(addr_or_opts) else: raise TypeError('只能接收ip:port格式或ChromiumOptions类型参数。') - return self._driver_options.debugger_address + return self._chromium_options.debugger_address def _run_browser(self): """连接浏览器""" - is_exist = connect_browser(self._driver_options) - ws = get(f'http://{self._driver_options.debugger_address}/json/version', + is_exist = connect_browser(self._chromium_options) + ws = get(f'http://{self._chromium_options.debugger_address}/json/version', headers={'Connection': 'close'}) if not ws: raise BrowserConnectError('\n浏览器连接失败,请检查是否启用全局代理。如有,须开放127.0.0.1地址。') ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1] - self._browser = Browser(self._driver_options.debugger_address, ws, self) + self._browser = Browser(self._chromium_options.debugger_address, ws, self) - if (is_exist and self._driver_options._headless is False and + if (is_exist and self._chromium_options._headless is False and 'headless' in self._browser.run_cdp('Browser.getVersion')['userAgent'].lower()): self._browser.quit(3) - connect_browser(self._driver_options) - ws = get(f'http://{self._driver_options.debugger_address}/json/version', headers={'Connection': 'close'}) + connect_browser(self._chromium_options) + ws = get(f'http://{self._chromium_options.debugger_address}/json/version', headers={'Connection': 'close'}) ws = ws.json()['webSocketDebuggerUrl'].split('/')[-1] - self._browser = Browser(self._driver_options.debugger_address, ws, self) + self._browser = Browser(self._chromium_options.debugger_address, ws, self) def _d_set_runtime_settings(self): """设置运行时用到的属性""" - self._timeouts = Timeout(self, - page_load=self._driver_options.timeouts['pageLoad'], - script=self._driver_options.timeouts['script'], - implicit=self._driver_options.timeouts['implicit']) - if self._driver_options.timeouts['implicit'] is not None: - self._timeout = self._driver_options.timeouts['implicit'] - self._load_mode = self._driver_options.load_mode - self._download_path = None if self._driver_options.download_path is None \ - else str(Path(self._driver_options.download_path).absolute()) + self._timeouts = Timeout(self, page_load=self._chromium_options.timeouts['pageLoad'], + script=self._chromium_options.timeouts['script'], + implicit=self._chromium_options.timeouts['implicit']) + if self._chromium_options.timeouts['implicit'] is not None: + self._timeout = self._chromium_options.timeouts['implicit'] + self._load_mode = self._chromium_options.load_mode + self._download_path = None if self._chromium_options.download_path is None \ + else str(Path(self._chromium_options.download_path).absolute()) + self.retry_times = self._chromium_options.retry_times + self.retry_interval = self._chromium_options.retry_interval def _page_init(self): """浏览器相关设置""" diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index c834405..b4bc381 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -20,7 +20,7 @@ class ChromiumPage(ChromiumBase): addr_or_opts: Union[str, int, ChromiumOptions] = None, tab_id: str = None, timeout: float = None): - self._driver_options: ChromiumOptions = ... + self._chromium_options: ChromiumOptions = ... self._browser: Browser = ... self._rect: Optional[TabRect] = ... diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index f7a5514..64eee41 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -274,7 +274,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): # s模式转d模式 if self._mode == 'd': if self._driver is None: - self._connect_browser(self.page._driver_options) + self._connect_browser(self.page._chromium_options) self._url = None if not self._has_driver else super(SessionPage, self).url self._has_driver = True diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index eed198f..7b5fe44 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -55,6 +55,8 @@ class SessionPage(BasePage): self._timeout = self._session_options.timeout self._download_path = None if self._session_options.download_path is None \ else str(Path(self._session_options.download_path).absolute()) + self.retry_times = self._session_options.retry_times + self.retry_interval = self._session_options.retry_interval def _create_session(self): """创建内建Session对象""" diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index e17d4ae..5e7db55 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -15,15 +15,15 @@ from .._units.setter import WebPageSetter class WebPage(SessionPage, ChromiumPage, BasePage): """整合浏览器和request的页面类""" - def __init__(self, mode='d', timeout=None, driver_options=None, session_or_options=None, driver_or_options=None): + def __init__(self, mode='d', timeout=None, chromium_options=None, session_or_options=None, driver_or_options=None): """初始化函数 :param mode: 'd' 或 's',即driver模式和session模式 :param timeout: 超时时间,d模式时为寻找元素时间,s模式时为连接时间,默认10秒 - :param driver_options: ChromiumDriver对象,只使用s模式时应传入False + :param chromium_options: ChromiumDriver对象,只使用s模式时应传入False :param session_or_options: Session对象或SessionOptions对象,只使用d模式时应传入False """ - if not driver_options and driver_or_options: - driver_options = driver_or_options + if not chromium_options and driver_or_options: + chromium_options = driver_or_options self._mode = mode.lower() if self._mode not in ('s', 'd'): raise ValueError('mode参数只能是s或d。') @@ -31,10 +31,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._has_session = True super().__init__(session_or_options=session_or_options) - if not driver_options: - driver_options = ChromiumOptions(read_file=driver_options) - driver_options.set_timeouts(implicit=self._timeout).set_paths(download_path=self.download_path) - super(SessionPage, self).__init__(addr_or_opts=driver_options, timeout=timeout) + if not chromium_options: + chromium_options = ChromiumOptions(read_file=chromium_options) + chromium_options.set_timeouts(implicit=self._timeout).set_paths(download_path=self.download_path) + super(SessionPage, self).__init__(addr_or_opts=chromium_options, timeout=timeout) self.change_mode(self._mode, go=False, copy_cookies=False) def __call__(self, loc_or_str, timeout=None): @@ -239,7 +239,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): # s模式转d模式 if self._mode == 'd': if self._driver is None: - self._connect_browser(self._driver_options) + self._connect_browser(self._chromium_options) self._url = None if not self._has_driver else super(SessionPage, self).url self._has_driver = True diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index 8f461d0..bdbd81f 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -25,13 +25,13 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def __init__(self, mode: str = 'd', timeout: float = None, - driver_options: Union[ChromiumOptions, bool] = None, + chromium_options: Union[ChromiumOptions, bool] = None, session_or_options: Union[Session, SessionOptions, bool] = None) -> None: self._mode: str = ... self._has_driver: bool = ... self._has_session: bool = ... self._session_options: Union[SessionOptions, None] = ... - self._driver_options: Union[ChromiumOptions, None] = ... + self._chromium_options: Union[ChromiumOptions, None] = ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement], diff --git a/setup.py b/setup.py index 1bd9af1..33e5c2b 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="4.0.0b12", + version="4.0.0b13", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",