This commit is contained in:
g1879 2022-01-12 16:30:12 +08:00
parent 0d0408a977
commit 7a9ff50f75
4 changed files with 44 additions and 33 deletions

View File

@ -449,22 +449,23 @@ class DriverOptions(Options):
""" """
super().__init__() super().__init__()
self._driver_path = None self._driver_path = None
self.ini_path = None self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini')
if read_file: if read_file:
self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini')
om = OptionsManager(self.ini_path) om = OptionsManager(self.ini_path)
options_dict = om.chrome_options options_dict = om.chrome_options
self._driver_path = om.paths.get('chromedriver_path', None)
self._binary_location = options_dict.get('binary_location', '') self._binary_location = options_dict.get('binary_location', '')
self._arguments = options_dict.get('arguments', []) self._arguments = options_dict.get('arguments', [])
self._extensions = options_dict.get('extensions', []) self._extensions = options_dict.get('extensions', [])
self._experimental_options = options_dict.get('experimental_options', {}) self._experimental_options = options_dict.get('experimental_options', {})
self._debugger_address = options_dict.get('debugger_address', None) 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.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 = options_dict.get('timeouts', {'implicit': 10, 'pageLoad': 30, 'script': 30})
self.timeouts['implicit'] *= 1000
self.timeouts['pageLoad'] *= 1000 self.timeouts['pageLoad'] *= 1000
self.timeouts['script'] *= 1000 self.timeouts['script'] *= 1000
@ -557,19 +558,19 @@ class DriverOptions(Options):
return self return self
def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> 'DriverOptions': def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> 'DriverOptions':
"""设置超时时间,selenium4以上版本有效 \n """设置超时时间,设置单位为秒,selenium4以上版本有效 \n
:param implicit: 查找元素超时时间 :param implicit: 查找元素超时时间
:param pageLoad: 页面加载超时时间 :param pageLoad: 页面加载超时时间
:param script: 脚本运行超时时间 :param script: 脚本运行超时时间
:return: 当前对象 :return: 当前对象
""" """
timeouts = self.timeouts timeouts = self._caps.get('timeouts', {'implicit': 10, 'pageLoad': 3000, 'script': 3000})
if implicit is not None: if implicit is not None:
timeouts['implicit'] = implicit timeouts['implicit'] = implicit
if pageLoad is not None: if pageLoad is not None:
timeouts['pageLoad'] = pageLoad timeouts['pageLoad'] = pageLoad * 1000
if script is not None: if script is not None:
timeouts['script'] = script timeouts['script'] = script * 1000
self.timeouts = timeouts self.timeouts = timeouts
return self return self
@ -629,7 +630,7 @@ class DriverOptions(Options):
:param value: 可接收 'normal', 'eager', 'none' :param value: 可接收 'normal', 'eager', 'none'
:return: 当前对象 :return: 当前对象
""" """
self.page_load_strategy = value self.page_load_strategy = value.lower()
return self return self
def set_paths(self, def set_paths(self,
@ -699,7 +700,7 @@ def _chrome_options_to_dict(options: Union[dict, DriverOptions, Options, None, b
except Exception: except Exception:
pass pass
if 'timeouts' in options_dir: if 'timeouts' in options_dir and 'timeouts' in options._caps:
timeouts = options.__getattribute__('timeouts') timeouts = options.__getattribute__('timeouts')
timeouts['pageLoad'] /= 1000 timeouts['pageLoad'] /= 1000
timeouts['script'] /= 1000 timeouts['script'] /= 1000

View File

@ -173,6 +173,13 @@ class DriverPage(BasePage):
self._timeout = second self._timeout = second
self._wait_object = None 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, def _try_to_connect(self,
to_url: str, to_url: str,
times: int = 0, times: int = 0,
@ -251,6 +258,22 @@ class DriverPage(BasePage):
"""返回当前焦点所在元素""" """返回当前焦点所在元素"""
return DriverElement(self.driver.switch_to.active_element, self) 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, def wait_ele(self,
loc_or_ele: Union[str, tuple, DriverElement, WebElement], loc_or_ele: Union[str, tuple, DriverElement, WebElement],
mode: str, mode: str,

View File

@ -46,14 +46,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
if self._mode not in ('s', 'd'): if self._mode not in ('s', 'd'):
raise ValueError('mode参数只能是s或d。') raise ValueError('mode参数只能是s或d。')
if driver_options: super(DriverPage, self).__init__(timeout)
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__()
self._driver, self._session = (None, True) if self._mode == 's' else (True, None) self._driver, self._session = (None, True) if self._mode == 's' else (True, None)
self._drission = drission or Drission(driver_options, session_options) self._drission = drission or Drission(driver_options, session_options)
self._wait_object = None self._wait_object = None
@ -62,6 +55,14 @@ class MixPage(SessionPage, DriverPage, BasePage):
if self._mode == 'd': if self._mode == 'd':
self._drission.driver # 接管或创建浏览器 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, def __call__(self,
loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
timeout: float = None) \ timeout: float = None) \
@ -352,20 +353,6 @@ class MixPage(SessionPage, DriverPage, BasePage):
self._response = None self._response = None
self.drission.close_session() 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的函数----------------------- # ----------------重写SessionPage的函数-----------------------
def post(self, def post(self,
url: str, url: str,

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="2.2.1", version="2.3.0",
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.",