优化标签页处理功能

This commit is contained in:
g1879 2020-08-12 16:24:14 +08:00
parent 45756a64b3
commit a8d4d3fb5d
3 changed files with 85 additions and 42 deletions

View File

@ -4,9 +4,9 @@
@Contact : g1879@qq.com @Contact : g1879@qq.com
@File : driver_page.py @File : driver_page.py
""" """
import time
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
from time import time
from typing import Union, List, Any from typing import Union, List, Any
from urllib.parse import quote from urllib.parse import quote
@ -183,8 +183,8 @@ class DriverPage(object):
raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement') raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement')
if is_ele: # 当传入参数是元素对象时 if is_ele: # 当传入参数是元素对象时
end_time = time.time() + timeout end_time = time() + timeout
while time.time() < end_time: while time() < end_time:
if mode == 'del': if mode == 'del':
try: try:
loc_or_ele.is_enabled() loc_or_ele.is_enabled()
@ -222,7 +222,10 @@ class DriverPage(object):
def get_tabs_sum(self) -> int: def get_tabs_sum(self) -> int:
"""返回标签页数量""" """返回标签页数量"""
try:
return len(self.driver.window_handles) return len(self.driver.window_handles)
except:
return 0
def get_tab_num(self) -> int: def get_tab_num(self) -> int:
"""返回当前标签页序号""" """返回当前标签页序号"""
@ -230,23 +233,28 @@ class DriverPage(object):
handle_list = self.driver.window_handles handle_list = self.driver.window_handles
return handle_list.index(handle) return handle_list.index(handle)
def create_tab(self): def create_tab(self, url: str = '') -> None:
"""新建并定位到一个标签页,该标签页在最后面""" """新建并定位到一个标签页,该标签页在最后面
:param url: 新标签页跳转到的网址
:return: None
"""
self.to_tab(-1) self.to_tab(-1)
self.run_script('window.open("");') self.run_script(f'window.open("{url}");')
self.to_tab(-1) self.to_tab(-1)
def close_current_tab(self) -> None: def close_current_tab(self) -> None:
"""关闭当前标签页""" """关闭当前标签页"""
self.driver.close() self.driver.close()
if self.get_tabs_sum():
self.to_tab(0)
def close_other_tabs(self, index: int = None) -> None: def close_other_tabs(self, index: int = None) -> None:
"""关闭序号以外标签页,没有传入序号代表保留当前页 \n """关闭序号以外标签页,没有传入序号代表保留当前页 \n
:param index: 要保留的标签页序号从0开始算 :param index: 要保留的标签页序号第一个为0最后为-1
:return: None :return: None
""" """
tabs = self.driver.window_handles # 获得所有标签页权柄 tabs = self.driver.window_handles # 获得所有标签页权柄
page_handle = tabs[index] if index >= 0 else self.driver.current_window_handle page_handle = tabs[index]
for i in tabs: # 遍历所有标签页,关闭非保留的 for i in tabs: # 遍历所有标签页,关闭非保留的
if i != page_handle: if i != page_handle:
self.driver.switch_to.window(i) self.driver.switch_to.window(i)
@ -255,7 +263,7 @@ class DriverPage(object):
def to_tab(self, index: int = 0) -> None: def to_tab(self, index: int = 0) -> None:
"""跳转到第几个标签页 \n """跳转到第几个标签页 \n
:param index: 标签页序号从0开始算 :param index: 标签页序号第一个为0最后为-1
:return: None :return: None
""" """
tabs = self.driver.window_handles # 获得所有标签页权柄 tabs = self.driver.window_handles # 获得所有标签页权柄
@ -263,8 +271,17 @@ class DriverPage(object):
def to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None: def to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None:
"""跳转到iframe \n """跳转到iframe \n
:param loc_or_ele: 可接收iframe序号(0开始)id或name控制字符串loc元组WebElement对象DriverElement对象 可接收iframe序号(0开始)id或name查询字符串loc元组WebElement对象DriverElement对象 \n
传入'main'跳到最高层传入'parent'跳到上一层 传入'main'跳到最高层传入'parent'跳到上一层 \n
示例 \n
to_iframe('tag:iframe') - 通过传入iframe的查询字符串定位 \n
to_iframe('iframe_id') - 通过iframe的id属性定位 \n
to_iframe('iframe_name') - 通过iframe的name属性定位 \n
to_iframe(iframe_element) - 通过传入元素对象定位 \n
to_iframe(0) - 通过iframe的序号定位 \n
to_iframe('main') - 跳到最高层 \n
to_iframe('parent') - 跳到上一层 \n
:param loc_or_ele: iframe的定位信息
:return: None :return: None
""" """
if isinstance(loc_or_ele, int): # 根据序号跳转 if isinstance(loc_or_ele, int): # 根据序号跳转

View File

@ -899,6 +899,16 @@ The following methods only take effect in driver mode, and will automatically sw
Returns the serial number of the current tab. Returns the serial number of the current tab.
### create_tab
create_tab(url: str = '') -> None
Create and locate a tab page, which is at the end.
Parameter Description:
- url - URL to jump in the new tab page
### close_current_tab ### close_current_tab
close_current_tab() -> None close_current_tab() -> None
@ -921,7 +931,7 @@ The following methods only take effect in driver mode, and will automatically sw
Jump to a tab page with a serial number. Jump to a tab page with a serial number.
Parameter Description: Parameter Description:
- index - The serial number of the target tab, start from 0 - index - The serial number of the target tab, start from 0
@ -936,10 +946,13 @@ Parameter Description:
- loc_or_ele - To search for iframe element conditions, you can receive iframe serial number (starting at 0), id or name, control string, loc parameter, WebElement object, DriverElement object, pass 'main' to jump to the top level, pass 'parent' to jump to parent level. - loc_or_ele - To search for iframe element conditions, you can receive iframe serial number (starting at 0), id or name, control string, loc parameter, WebElement object, DriverElement object, pass 'main' to jump to the top level, pass 'parent' to jump to parent level.
Examples: Examples:
- to_iframe('@id:iframe_id') - to_iframe('tag:iframe') - Positioning by the query string passed in the iframe
- to_iframe(iframe_element) - to_iframe('iframe_id') - Positioning by the id attribute of the iframe
- to_iframe(0) - to_iframe('iframe_name') - Positioning by the name attribute of the iframe
- to_iframe('iframe_name') - to_iframe(iframe_element) - Positioning by passing in the element object
- to_iframe(0) - Positioning by the serial number of the iframe
- to_iframe('main') - Switch to the top level
- to_iframe('parent') - Switch to the previous level
### scroll_to_see ### scroll_to_see

View File

@ -898,6 +898,16 @@ MixPage封装了页面操作的常用功能可在driver和session模式间无
返回当前标签页序号。 返回当前标签页序号。
### create_tab
create_tab(url: str = '') -> None
新建并定位到一个标签页,该标签页在最后面。
参数说明:
- url - 新标签页跳转到的网址
### close_current_tab ### close_current_tab
close_current_tab() -> None close_current_tab() -> None
@ -935,10 +945,13 @@ MixPage封装了页面操作的常用功能可在driver和session模式间无
- loc_or_ele - 查找iframe元素的条件可接收iframe序号(0开始)、id或name、控制字符串、loc参数、WebElement对象、DriverElement对象传入'main'跳到最高层,传入'parent'跳到上一层。 - loc_or_ele - 查找iframe元素的条件可接收iframe序号(0开始)、id或name、控制字符串、loc参数、WebElement对象、DriverElement对象传入'main'跳到最高层,传入'parent'跳到上一层。
示例: 示例:
- to_iframe('@id:iframe_id') - to_iframe('tag:iframe') - 通过传入iframe的查询字符串定位
- to_iframe(iframe_element) - to_iframe('iframe_id') - 通过iframe的id属性定位
- to_iframe(0) - to_iframe('iframe_name') - 通过iframe的name属性定位
- to_iframe('iframe_name') - to_iframe(iframe_element) - 通过传入元素对象定位
- to_iframe(0) - 通过iframe的序号定位
- to_iframe('main') - 跳到最高层
- to_iframe('parent') - 跳到上一层
### scroll_to_see ### scroll_to_see