mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
2.4.1
This commit is contained in:
parent
59764e8305
commit
6cc2336408
@ -916,17 +916,12 @@ class Select(object):
|
||||
self.inner_ele = ele
|
||||
self.select_ele = Select(ele.inner_ele)
|
||||
|
||||
def __call__(self,
|
||||
text_value_index: Union[str, int, list, tuple] = None,
|
||||
para_type: str = 'text',
|
||||
deselect: bool = False) -> bool:
|
||||
"""选定或取消选定下拉列表中子元素 \n
|
||||
:param text_value_index: 根据文本、值选或序号择选项,若允许多选,传入list或tuple可多选
|
||||
:param para_type: 参数类型,可选 'text'、'value'、'index'
|
||||
:param deselect: 是否取消选择
|
||||
:return: 是否选择成功
|
||||
def __call__(self, text_or_index: Union[str, int, list, tuple]) -> bool:
|
||||
"""选定下拉列表中子元素 \n
|
||||
:param text_or_index: 根据文本、值选或序号择选项,若允许多选,传入list或tuple可多选
|
||||
:return: None
|
||||
"""
|
||||
return self.select(text_value_index, para_type, deselect)
|
||||
return self.select(text_or_index)
|
||||
|
||||
@property
|
||||
def is_multi(self) -> bool:
|
||||
@ -957,10 +952,48 @@ class Select(object):
|
||||
"""清除所有已选项"""
|
||||
self.select_ele.deselect_all()
|
||||
|
||||
def select(self,
|
||||
text_value_index: Union[str, int, list, tuple] = None,
|
||||
para_type: str = 'text',
|
||||
deselect: bool = False) -> bool:
|
||||
def select(self, text_or_index: Union[str, int, list, tuple]) -> bool:
|
||||
"""选定下拉列表中子元素 \n
|
||||
:param text_or_index: 根据文本、值选或序号择选项,若允许多选,传入list或tuple可多选
|
||||
:return: 是否选择成功
|
||||
"""
|
||||
i = 'index' if isinstance(text_or_index, int) else 'text'
|
||||
return self._select(text_or_index, i, False)
|
||||
|
||||
def select_by_value(self, value: Union[str, list, tuple]) -> bool:
|
||||
"""此方法用于根据value值选择项。当元素是多选列表时,可以接收list或tuple \n
|
||||
:param value: value属性值,传入list或tuple可选择多项
|
||||
:return: None
|
||||
"""
|
||||
return self._select(value, 'value', False)
|
||||
|
||||
def deselect(self, text_or_index: Union[str, int, list, tuple]) -> bool:
|
||||
"""取消选定下拉列表中子元素 \n
|
||||
:param text_or_index: 根据文本或序号取消择选项,若允许多选,传入list或tuple可取消多项
|
||||
:return: None
|
||||
"""
|
||||
i = 'index' if isinstance(text_or_index, int) else 'text'
|
||||
return self._select(text_or_index, i, True)
|
||||
|
||||
def deselect_by_value(self, value: Union[str, list, tuple]) -> bool:
|
||||
"""此方法用于根据value值取消选择项。当元素是多选列表时,可以接收list或tuple \n
|
||||
:param value: value属性值,传入list或tuple可取消多项
|
||||
:return: None
|
||||
"""
|
||||
return self._select(value, 'value', True)
|
||||
|
||||
def invert(self) -> None:
|
||||
"""反选"""
|
||||
if not self.is_multi:
|
||||
raise NotImplementedError("只能对多项选框执行反选。")
|
||||
|
||||
for i in self.options:
|
||||
i.click(by_js=True)
|
||||
|
||||
def _select(self,
|
||||
text_value_index: Union[str, int, list, tuple] = None,
|
||||
para_type: str = 'text',
|
||||
deselect: bool = False) -> bool:
|
||||
"""选定或取消选定下拉列表中子元素 \n
|
||||
:param text_value_index: 根据文本、值选或序号择选项,若允许多选,传入list或tuple可多选
|
||||
:param para_type: 参数类型,可选 'text'、'value'、'index'
|
||||
@ -995,15 +1028,15 @@ class Select(object):
|
||||
return False
|
||||
|
||||
elif isinstance(text_value_index, (list, tuple)):
|
||||
self.select_multi(text_value_index, para_type, deselect)
|
||||
return self._select_multi(text_value_index, para_type, deselect)
|
||||
|
||||
else:
|
||||
raise TypeError('只能传入str、int、list和tuple类型。')
|
||||
|
||||
def select_multi(self,
|
||||
text_value_index: Union[list, tuple] = None,
|
||||
para_type: str = 'text',
|
||||
deselect: bool = False) -> Union[bool, list]:
|
||||
def _select_multi(self,
|
||||
text_value_index: Union[list, tuple] = None,
|
||||
para_type: str = 'text',
|
||||
deselect: bool = False) -> bool:
|
||||
"""选定或取消选定下拉列表中多个子元素 \n
|
||||
:param text_value_index: 根据文本、值选或序号择选多项
|
||||
:param para_type: 参数类型,可选 'text'、'value'、'index'
|
||||
@ -1013,47 +1046,19 @@ class Select(object):
|
||||
if para_type not in ('text', 'value', 'index'):
|
||||
raise ValueError('para_type参数只能传入“text”、“value”或“index”')
|
||||
|
||||
if isinstance(text_value_index, (list, tuple)):
|
||||
fail_list = []
|
||||
for i in text_value_index:
|
||||
if not isinstance(i, (int, str)):
|
||||
raise TypeError('列表只能由str或int组成')
|
||||
|
||||
if not self.select(i, para_type, deselect):
|
||||
fail_list.append(i)
|
||||
|
||||
return fail_list or True
|
||||
|
||||
else:
|
||||
if not isinstance(text_value_index, (list, tuple)):
|
||||
raise TypeError('只能传入list或tuple类型。')
|
||||
|
||||
def deselect(self,
|
||||
text_value_index: Union[str, int, list, tuple] = None,
|
||||
para_type: str = 'text') -> bool:
|
||||
"""取消选定下拉列表中子元素 \n
|
||||
:param text_value_index: 根据文本、值选或序号择选项,若允许多选,传入list或tuple可多选
|
||||
:param para_type: 参数类型,可选 'text'、'value'、'index'
|
||||
:return: 是否选择成功
|
||||
"""
|
||||
return self.select(text_value_index, para_type, True)
|
||||
success = True
|
||||
for i in text_value_index:
|
||||
if not isinstance(i, (int, str)):
|
||||
raise TypeError('列表只能由str或int组成')
|
||||
|
||||
def deselect_multi(self,
|
||||
text_value_index: Union[list, tuple] = None,
|
||||
para_type: str = 'text') -> Union[bool, list]:
|
||||
"""取消选定下拉列表中多个子元素 \n
|
||||
:param text_value_index: 根据文本、值选或序号取消择选多项
|
||||
:param para_type: 参数类型,可选 'text'、'value'、'index'
|
||||
:return: 是否选择成功
|
||||
"""
|
||||
return self.select_multi(text_value_index, para_type, True)
|
||||
p = 'index' if isinstance(i, int) else para_type
|
||||
if not self._select(i, p, deselect):
|
||||
success = False
|
||||
|
||||
def invert(self) -> None:
|
||||
"""反选"""
|
||||
if not self.is_multi:
|
||||
raise NotImplementedError("只能对多项选框执行反选。")
|
||||
|
||||
for i in self.options:
|
||||
i.click()
|
||||
return success
|
||||
|
||||
|
||||
class ElementWaiter(object):
|
||||
|
@ -404,14 +404,14 @@ options = ele.select.select.selected_options
|
||||
|
||||
### select()
|
||||
|
||||
该方法用于选定或取消选定下拉列表中子元素。Select 类的`__call__()`方法直接调用这个方法,因此可以直接`ele.select()`来替代这个方法。写法更直观。
|
||||
当元素是多选列表时,可以接受`list`或`tuple`,同时选择多个项。
|
||||
该方法用于选定下拉列表中子元素。
|
||||
接收`int`类型时根据序号选择,接收`str`类型时根据文本选择。
|
||||
接收`list`或`tuple`时同时选择多个项,多项可序号和文本混排。只能在多选列表使用。
|
||||
Select 类的`__call__()`方法直接调用这个方法,因此可以直接`ele.select()`来替代这个方法。写法更直观。
|
||||
|
||||
参数:
|
||||
|
||||
- text_value_index:根据文本、值选或序号择选项,若允许多选,传入`list`或`tuple`可多选
|
||||
- para_type:参数类型,可选`'text'`、`'value'`、`'index'`,默认根据文本选择
|
||||
- deselect:是否取消选择
|
||||
- text_or_index:根据文本或序号择选项,若允许多选,传入`list`或`tuple`可多选
|
||||
|
||||
返回:是否选择成功
|
||||
|
||||
@ -419,20 +419,35 @@ options = ele.select.select.selected_options
|
||||
# 根据文本选择下拉列表项
|
||||
ele.select('text')
|
||||
|
||||
# 根据 value 选择下拉列表项
|
||||
ele.select(value, 'value')
|
||||
|
||||
# 根据序号选择下拉列表项
|
||||
ele.select(index, 'index')
|
||||
# 选择第一个下拉列表项
|
||||
ele.select(0)
|
||||
|
||||
# 选择多个文本项
|
||||
ele.select(('text1', 'text2'))
|
||||
|
||||
# 选择多个 value 项
|
||||
ele.select(('value1', 'value2'), 'value')
|
||||
|
||||
# 选择多个序号
|
||||
ele.select(('index1', 'index2'), 'index')
|
||||
ele.select((0, 2))
|
||||
|
||||
# 文本和序号混合选择
|
||||
ele.select(('text1', 2))
|
||||
```
|
||||
|
||||
### select_by_value()
|
||||
|
||||
参数:
|
||||
|
||||
- value:`value`属性值,若允许多选,传入`list`或`tuple`可多选
|
||||
|
||||
返回:是否选择成功
|
||||
|
||||
此方法用于根据`value`值选择项。当元素是多选列表时,可以接受`list`或`tuple`,同时选择多个项,可和序号混排。
|
||||
|
||||
```python
|
||||
# 根据 value 值选择列表项
|
||||
ele.select.select_by_value('value1')
|
||||
|
||||
# 根据 value 值选择多个项
|
||||
ele.select.select_by_value(('value1', 2))
|
||||
```
|
||||
|
||||
### deselect()
|
||||
@ -441,29 +456,43 @@ ele.select(('index1', 'index2'), 'index')
|
||||
|
||||
参数:
|
||||
|
||||
- text_value_index:根据文本、值选或序号择选项,若允许多选,传入`list`或`tuple`可多选
|
||||
- para_type:参数类型,可选` 'text'`、`'value'`、`'index'`
|
||||
- text_or_index:根据文本、值选或序号择选项,若允许多选,传入`list`或`tuple`可多选
|
||||
|
||||
返回:是否选择成功
|
||||
返回:是否取消选择成功
|
||||
|
||||
```python
|
||||
# 根据文本取消选择下拉列表项
|
||||
ele.select.deselect('text')
|
||||
ele.select.deselect('text')
|
||||
|
||||
# 根据 value 取消选择下拉列表项
|
||||
ele.select.deselect(value, 'value')
|
||||
|
||||
# 根据序号取消选择下拉列表项
|
||||
ele.select.deselect(index, 'index')
|
||||
# 取消选择第一项
|
||||
ele.select.deselect(0)
|
||||
|
||||
# 取消选择多个文本项
|
||||
ele.select.deselect(('text1', 'text2'))
|
||||
|
||||
# 取消选择多个 value 项
|
||||
ele.select.deselect(('value1', 'value2'), 'value')
|
||||
|
||||
# 取消选择多个序号
|
||||
ele.select.deselect(('index1', 'index2'), 'index')
|
||||
ele.select.deselect((0, 1))
|
||||
|
||||
# 文本和序号混合取消选择
|
||||
ele.select.deselect(('text1', 2))
|
||||
```
|
||||
|
||||
### deselect_by_value()
|
||||
|
||||
此方法用于根据`value`值取消选择项。当元素是多选列表时,可以接受`list`或`tuple`,同时取消选择多个项,可和序号混排。
|
||||
|
||||
参数:
|
||||
|
||||
- value:`value`属性值,若允许多选,传入`list`或`tuple`可取消多项
|
||||
|
||||
返回:是否取消选择成功
|
||||
|
||||
```python
|
||||
# 根据 value 值取消选择列表项
|
||||
ele.select.deselect_by_value('value1')
|
||||
|
||||
# 根据 value 值取消选择多个项
|
||||
ele.select.deselect_by_value(('value1', 2))
|
||||
```
|
||||
|
||||
## 多项列表独有功能
|
||||
@ -480,7 +509,7 @@ ele.select.deselect(('index1', 'index2'), 'index')
|
||||
ele.select.clear()
|
||||
```
|
||||
|
||||
### invert
|
||||
### invert()
|
||||
|
||||
此方法用于反选多选列表选项。
|
||||
|
||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
||||
|
||||
setup(
|
||||
name="DrissionPage",
|
||||
version="2.4.0",
|
||||
version="2.4.1",
|
||||
author="g1879",
|
||||
author_email="g1879@qq.com",
|
||||
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
||||
|
Loading…
x
Reference in New Issue
Block a user