mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
d模式时创建MixPage就接管或创建浏览器
This commit is contained in:
parent
2ddbaca3f9
commit
01b8d2c0ec
@ -87,6 +87,12 @@ class Drission(object):
|
||||
|
||||
@property
|
||||
def driver(self) -> WebDriver:
|
||||
"""返回WebDriver对象,如未初始化则按配置信息创建。 \n
|
||||
如设置了本地调试浏览器,可自动接入或打开浏览器进程。
|
||||
"""
|
||||
return self.get_driver()
|
||||
|
||||
def get_driver(self) -> WebDriver:
|
||||
"""返回WebDriver对象,如未初始化则按配置信息创建。 \n
|
||||
如设置了本地调试浏览器,可自动接入或打开浏览器进程。
|
||||
"""
|
||||
|
@ -470,7 +470,7 @@ class DriverElement(DrissionElement):
|
||||
return True
|
||||
|
||||
def run_script(self, script: str, *args) -> Any:
|
||||
"""执行js代码,传入自己为第一个参数 \n
|
||||
"""执行js代码,代码中用arguments[0]表示自己 \n
|
||||
:param script: js文本
|
||||
:param args: 传入的参数
|
||||
:return: js执行结果
|
||||
|
@ -292,8 +292,22 @@ class DriverPage(BasePage):
|
||||
self.run_script(f'window.open("{url}");')
|
||||
self.to_tab(-1)
|
||||
|
||||
def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
|
||||
"""关闭传入的标签页,默认关闭当前页。可传入多个 \n
|
||||
注意:当程序使用的是截关的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。
|
||||
:param num_or_handles:要关闭的标签页序号或handle,可传入handle和序号组成的列表或元组,为None时关闭当前页
|
||||
:return: None
|
||||
"""
|
||||
tabs = (self.current_tab_handle,) if num_or_handles is None else _get_handles(self.tab_handles, num_or_handles)
|
||||
for i in tabs:
|
||||
self.driver.switch_to.window(i)
|
||||
self.driver.close()
|
||||
|
||||
self.to_tab(0)
|
||||
|
||||
def close_other_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
|
||||
"""关闭传入的标签页以外标签页,默认保留当前页。可传入列表或元组 \n
|
||||
"""关闭传入的标签页以外标签页,默认保留当前页。可传入多个 \n
|
||||
注意:当程序使用的是截关的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。
|
||||
:param num_or_handles: 要保留的标签页序号或handle,可传入handle和序号组成的列表或元组,为None时保存当前页
|
||||
:return: None
|
||||
"""
|
||||
@ -306,18 +320,6 @@ class DriverPage(BasePage):
|
||||
|
||||
self.to_tab(0)
|
||||
|
||||
def close_tab(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
|
||||
"""关闭传入的标签页,默认关闭当前页。可传入列表或元组 \n
|
||||
:param num_or_handles:要关闭的标签页序号或handle,可传入handle和序号组成的列表或元组,为None时关闭当前页
|
||||
:return: None
|
||||
"""
|
||||
tabs = (self.current_tab_handle,) if num_or_handles is None else _get_handles(self.tab_handles, num_or_handles)
|
||||
for i in tabs:
|
||||
self.driver.switch_to.window(i)
|
||||
self.driver.close()
|
||||
|
||||
self.to_tab(0)
|
||||
|
||||
def to_tab(self, num_or_handle: Union[int, str] = 0) -> None:
|
||||
"""跳转到标签页 \n
|
||||
:param num_or_handle: 标签页序号或handle字符串,序号第一个为0,最后为-1
|
||||
|
@ -41,13 +41,19 @@ class MixPage(SessionPage, DriverPage, BasePage):
|
||||
:param driver_options: 浏览器设置,没传入drission参数时会用这个设置新建Drission对象中的WebDriver对象,传入False则不创建
|
||||
:param session_options: requests设置,没传入drission参数时会用这个设置新建Drission对象中的Session对象,传入False则不创建
|
||||
"""
|
||||
super(DriverPage, self).__init__(timeout) # BasePage的__init__()
|
||||
self._mode = mode.lower()
|
||||
if self._mode not in ('s', 'd'):
|
||||
raise ValueError('mode参数只能是s或d。')
|
||||
|
||||
super(DriverPage, self).__init__(timeout) # BasePage的__init__()
|
||||
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
|
||||
self._response = None
|
||||
|
||||
if self._mode == 'd':
|
||||
self._drission.get_driver() # 接管或创建浏览器
|
||||
|
||||
def __call__(self,
|
||||
loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
|
||||
timeout: float = None) \
|
||||
|
Loading…
x
Reference in New Issue
Block a user