diff --git a/DrissionPage/config.py b/DrissionPage/config.py index ec76d00..64dccb3 100644 --- a/DrissionPage/config.py +++ b/DrissionPage/config.py @@ -449,22 +449,23 @@ class DriverOptions(Options): """ super().__init__() self._driver_path = None - self.ini_path = None + self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini') if read_file: - self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini') om = OptionsManager(self.ini_path) options_dict = om.chrome_options + self._driver_path = om.paths.get('chromedriver_path', None) self._binary_location = options_dict.get('binary_location', '') self._arguments = options_dict.get('arguments', []) self._extensions = options_dict.get('extensions', []) self._experimental_options = options_dict.get('experimental_options', {}) self._debugger_address = options_dict.get('debugger_address', None) - self._driver_path = om.paths.get('chromedriver_path', None) self.set_window_rect = options_dict.get('set_window_rect', None) - self.page_load_strategy = om.paths.get('page_load_strategy', 'normal') + self.page_load_strategy = options_dict.get('page_load_strategy', 'normal') + self.timeouts = options_dict.get('timeouts', {'implicit': 10, 'pageLoad': 30, 'script': 30}) + self.timeouts['implicit'] *= 1000 self.timeouts['pageLoad'] *= 1000 self.timeouts['script'] *= 1000 @@ -557,19 +558,19 @@ class DriverOptions(Options): return self def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> 'DriverOptions': - """设置超时时间,selenium4以上版本有效 \n + """设置超时时间,设置单位为秒,selenium4以上版本有效 \n :param implicit: 查找元素超时时间 :param pageLoad: 页面加载超时时间 :param script: 脚本运行超时时间 :return: 当前对象 """ - timeouts = self.timeouts + timeouts = self._caps.get('timeouts', {'implicit': 10, 'pageLoad': 3000, 'script': 3000}) if implicit is not None: timeouts['implicit'] = implicit if pageLoad is not None: - timeouts['pageLoad'] = pageLoad + timeouts['pageLoad'] = pageLoad * 1000 if script is not None: - timeouts['script'] = script + timeouts['script'] = script * 1000 self.timeouts = timeouts return self @@ -629,7 +630,7 @@ class DriverOptions(Options): :param value: 可接收 'normal', 'eager', 'none' :return: 当前对象 """ - self.page_load_strategy = value + self.page_load_strategy = value.lower() return self def set_paths(self, @@ -699,7 +700,7 @@ def _chrome_options_to_dict(options: Union[dict, DriverOptions, Options, None, b except Exception: pass - if 'timeouts' in options_dir: + if 'timeouts' in options_dir and 'timeouts' in options._caps: timeouts = options.__getattribute__('timeouts') timeouts['pageLoad'] /= 1000 timeouts['script'] /= 1000 diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 38367d3..3bb61ad 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -173,6 +173,13 @@ class DriverPage(BasePage): self._timeout = second self._wait_object = None + @property + def timeouts(self) -> dict: + """返回三种超时时间,selenium4以上版本可用""" + return {'implicit': self.timeout, + 'pageLoad': self.driver.timeouts.page_load, + 'script': self.driver.timeouts.script} + def _try_to_connect(self, to_url: str, times: int = 0, @@ -251,6 +258,22 @@ class DriverPage(BasePage): """返回当前焦点所在元素""" return DriverElement(self.driver.switch_to.active_element, self) + def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> None: + """设置超时时间,单位为秒,selenium4以上版本有效 \n + :param implicit: 查找元素超时时间 + :param pageLoad: 页面加载超时时间 + :param script: 脚本运行超时时间 + :return: None + """ + if implicit is not None: + self.timeout = implicit + + if pageLoad is not None: + self.driver.set_page_load_timeout(pageLoad) + + if script is not None: + self.driver.set_script_timeout(script) + def wait_ele(self, loc_or_ele: Union[str, tuple, DriverElement, WebElement], mode: str, diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index dc2c215..c24a3da 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -46,14 +46,7 @@ class MixPage(SessionPage, DriverPage, BasePage): if self._mode not in ('s', 'd'): raise ValueError('mode参数只能是s或d。') - if driver_options: - try: - timeout = driver_options.timeouts.get('implicit', None) - except Exception: - timeout = None - timeout = timeout if timeout is not None else 10 - - super(DriverPage, self).__init__(timeout) # BasePage的__init__() + super(DriverPage, self).__init__(timeout) self._driver, self._session = (None, True) if self._mode == 's' else (True, None) self._drission = drission or Drission(driver_options, session_options) self._wait_object = None @@ -62,6 +55,14 @@ class MixPage(SessionPage, DriverPage, BasePage): if self._mode == 'd': self._drission.driver # 接管或创建浏览器 + try: + timeouts = self.drission.driver_options.timeouts + t = timeout if timeout is not None else timeouts['implicit'] / 1000 + self.set_timeouts(t, timeouts['pageLoad'] / 1000, timeouts['script'] / 1000) + + except Exception: + self.timeout = timeout if timeout is not None else 10 + def __call__(self, loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], timeout: float = None) \ @@ -352,20 +353,6 @@ class MixPage(SessionPage, DriverPage, BasePage): self._response = None self.drission.close_session() - def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> None: - """设置超时时间,selenium4以上版本有效 \n - :param implicit: 查找元素超时时间 - :param pageLoad: 页面加载超时时间 - :param script: 脚本运行超时时间 - :return: 当前对象 - """ - if implicit is not None: - self.timeout = implicit - if pageLoad is not None: - self.driver.timeouts.page_load = pageLoad - if script is not None: - self.driver.timeouts.script = script - # ----------------重写SessionPage的函数----------------------- def post(self, url: str, diff --git a/setup.py b/setup.py index e84ed03..5ce03f8 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="2.2.1", + version="2.3.0", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",