优化标签页处理功能

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
@File : driver_page.py
"""
import time
from glob import glob
from pathlib import Path
from time import time
from typing import Union, List, Any
from urllib.parse import quote
@ -79,15 +79,15 @@ class DriverPage(object):
mode: str = None,
timeout: float = None,
show_errmsg: bool = False) -> Union[DriverElement, List[DriverElement], None]:
"""返回页面中符合条件的元素,默认返回第一个 \n
"""返回页面中符合条件的元素,默认返回第一个 \n
示例 \n
- 接收到元素对象时 \n
返回DriverElement对象 \n
- 用loc元组查找 \n
ele.ele((By.CLASS_NAME, 'ele_class')) - 返回所有class为ele_class的子元素 \n
- 用查询字符串查找 \n
查找方式属性tag name和属性文本xpathcss selector \n
其中@表示属性=表示精确匹配:表示模糊匹配无控制字符串时默认搜索该字符串 \n
- 用loc元组查找 \n
ele.ele((By.CLASS_NAME, 'ele_class')) - 返回所有class为ele_class的子元素 \n
- 用查询字符串查找 \n
查找方式属性tag name和属性文本xpathcss selector \n
其中@表示属性=表示精确匹配:表示模糊匹配无控制字符串时默认搜索该字符串 \n
page.ele('@class:ele_class') - 返回第一个class含有ele_class的元素 \n
page.ele('@name=ele_name') - 返回第一个name等于ele_name的元素 \n
page.ele('@placeholder') - 返回第一个带placeholder属性的元素 \n
@ -122,19 +122,19 @@ class DriverPage(object):
return execute_driver_find(self.driver, loc_or_ele, mode, show_errmsg, timeout)
def eles(self, loc_or_str: Union[tuple, str], timeout: float = None, show_errmsg=False) -> List[DriverElement]:
"""返回页面中所有符合条件的元素 \n
示例 \n
- 用loc元组查找 \n
page.eles((By.CLASS_NAME, 'ele_class')) - 返回所有class为ele_class的元素 \n
- 用查询字符串查找 \n
"""返回页面中所有符合条件的元素 \n
示例 \n
- 用loc元组查找 \n
page.eles((By.CLASS_NAME, 'ele_class')) - 返回所有class为ele_class的元素 \n
- 用查询字符串查找 \n
查找方式属性tag name和属性文本xpathcss selector \n
其中@表示属性=表示精确匹配:表示模糊匹配无控制字符串时默认搜索该字符串 \n
page.eles('@class:ele_class') - 返回所有class含有ele_class的元素 \n
page.eles('@name=ele_name') - 返回所有name等于ele_name的元素 \n
page.eles('@placeholder') - 返回所有带placeholder属性的元素 \n
page.eles('tag:p') - 返回所有<p>元素 \n
page.eles('tag:div@class:ele_class') - 返回所有class含有ele_class的div元素 \n
page.eles('tag:div@class=ele_class') - 返回所有class等于ele_class的div元素 \n
其中@表示属性=表示精确匹配:表示模糊匹配无控制字符串时默认搜索该字符串 \n
page.eles('@class:ele_class') - 返回所有class含有ele_class的元素 \n
page.eles('@name=ele_name') - 返回所有name等于ele_name的元素 \n
page.eles('@placeholder') - 返回所有带placeholder属性的元素 \n
page.eles('tag:p') - 返回所有<p>元素 \n
page.eles('tag:div@class:ele_class') - 返回所有class含有ele_class的div元素 \n
page.eles('tag:div@class=ele_class') - 返回所有class等于ele_class的div元素 \n
page.eles('tag:div@text():some_text') - 返回所有文本含有some_text的div元素 \n
page.eles('tag:div@text()=some_text') - 返回所有文本等于some_text的div元素 \n
page.eles('text:some_text') - 返回所有文本含有some_text的元素 \n
@ -183,8 +183,8 @@ class DriverPage(object):
raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement')
if is_ele: # 当传入参数是元素对象时
end_time = time.time() + timeout
while time.time() < end_time:
end_time = time() + timeout
while time() < end_time:
if mode == 'del':
try:
loc_or_ele.is_enabled()
@ -222,7 +222,10 @@ class DriverPage(object):
def get_tabs_sum(self) -> int:
"""返回标签页数量"""
return len(self.driver.window_handles)
try:
return len(self.driver.window_handles)
except:
return 0
def get_tab_num(self) -> int:
"""返回当前标签页序号"""
@ -230,23 +233,28 @@ class DriverPage(object):
handle_list = self.driver.window_handles
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.run_script('window.open("");')
self.run_script(f'window.open("{url}");')
self.to_tab(-1)
def close_current_tab(self) -> None:
"""关闭当前标签页"""
self.driver.close()
if self.get_tabs_sum():
self.to_tab(0)
def close_other_tabs(self, index: int = None) -> None:
"""关闭序号以外标签页,没有传入序号代表保留当前页 \n
:param index: 要保留的标签页序号从0开始算
"""关闭序号以外标签页,没有传入序号代表保留当前页 \n
:param index: 要保留的标签页序号第一个为0最后为-1
:return: None
"""
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: # 遍历所有标签页,关闭非保留的
if i != page_handle:
self.driver.switch_to.window(i)
@ -254,17 +262,26 @@ class DriverPage(object):
self.driver.switch_to.window(page_handle) # 把权柄定位回保留的页面
def to_tab(self, index: int = 0) -> None:
"""跳转到第几个标签页 \n
:param index: 标签页序号从0开始算
"""跳转到第几个标签页 \n
:param index: 标签页序号第一个为0最后为-1
:return: None
"""
tabs = self.driver.window_handles # 获得所有标签页权柄
self.driver.switch_to.window(tabs[index])
def to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None:
"""跳转到iframe \n
:param loc_or_ele: 可接收iframe序号(0开始)id或name控制字符串loc元组WebElement对象DriverElement对象
传入'main'跳到最高层传入'parent'跳到上一层
"""跳转到iframe \n
可接收iframe序号(0开始)id或name查询字符串loc元组WebElement对象DriverElement对象 \n
传入'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
"""
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.
### 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() -> 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.
Parameter Description:
Parameter Description:
- 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.
Examples:
- to_iframe('@id:iframe_id')
- to_iframe(iframe_element)
- to_iframe(0)
- to_iframe('iframe_name')
- to_iframe('tag:iframe') - Positioning by the query string passed in the iframe
- to_iframe('iframe_id') - Positioning by the id attribute of the iframe
- to_iframe('iframe_name') - Positioning by the name attribute of the iframe
- 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

View File

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