2.5.6 优化提示框处理、拖拽、清除文本框

This commit is contained in:
g1879 2022-03-16 09:54:59 +08:00
parent 7e0e35a463
commit 96e5475c06
5 changed files with 25 additions and 25 deletions

View File

@ -586,12 +586,12 @@ class DriverElement(DrissionElement):
except Exception: except Exception:
pass pass
def clear(self, insure_clear: bool = True) -> Union[None, bool]: def clear(self, insure: bool = True) -> Union[None, bool]:
"""清空元素文本 \n """清空元素文本 \n
:param insure_clear: 是否确保清空 :param insure: 是否确保清空
:return: 是否清空成功不能清空的元素返回None :return: 是否清空成功不能清空的元素返回None
""" """
if insure_clear: if insure:
return self.input('') return self.input('')
else: else:
@ -694,7 +694,7 @@ class DriverElement(DrissionElement):
:param y: y变化值 :param y: y变化值
:param speed: 拖动的速度传入0即瞬间到达 :param speed: 拖动的速度传入0即瞬间到达
:param shake: 是否随机抖动 :param shake: 是否随机抖动
:return: 是否推拽成功 :return: None
""" """
x += self.location['x'] + self.size['width'] // 2 x += self.location['x'] + self.size['width'] // 2
y += self.location['y'] + self.size['height'] // 2 y += self.location['y'] + self.size['height'] // 2
@ -708,7 +708,7 @@ class DriverElement(DrissionElement):
:param ele_or_loc: 另一个元素或坐标元组坐标为元素中点的坐标 :param ele_or_loc: 另一个元素或坐标元组坐标为元素中点的坐标
:param speed: 拖动的速度传入0即瞬间到达 :param speed: 拖动的速度传入0即瞬间到达
:param shake: 是否随机抖动 :param shake: 是否随机抖动
:return: 是否拖拽成功 :return: None
""" """
# x, y目标点坐标 # x, y目标点坐标
if isinstance(ele_or_loc, (DriverElement, WebElement)): if isinstance(ele_or_loc, (DriverElement, WebElement)):

View File

@ -462,10 +462,10 @@ class DriverPage(BasePage):
""" """
return glob(f'{download_path}{sep}*.crdownload') return glob(f'{download_path}{sep}*.crdownload')
def process_alert(self, mode: str = 'ok', text: str = None, timeout: float = None) -> Union[str, None]: def process_alert(self, ok: bool = True, send: str = None, timeout: float = None) -> Union[str, None]:
"""处理提示框 \n """处理提示框 \n
:param mode: 'ok' 'cancel'若输入其它值不会按按钮但依然返回文本值 :param ok: True表示确认False表示取消其它值不会按按钮但依然返回文本值
:param text: 处理prompt提示框时可输入文本 :param send: 处理prompt提示框时可输入文本
:param timeout: 等待提示框出现的超时时间 :param timeout: 等待提示框出现的超时时间
:return: 提示框内容文本未等到提示框则返回None :return: 提示框内容文本未等到提示框则返回None
""" """
@ -479,23 +479,23 @@ class DriverPage(BasePage):
timeout = timeout if timeout is not None else self.timeout timeout = timeout if timeout is not None else self.timeout
t1 = perf_counter() t1 = perf_counter()
alert = do_it() alert = do_it()
while not alert and perf_counter() - t1 <= timeout: while alert is False and perf_counter() - t1 <= timeout:
alert = do_it() alert = do_it()
if not alert: if alert is False:
return None return None
if text: res_text = alert.text
alert.send_keys(text)
text = alert.text if send is not None:
alert.send_keys(send)
if mode == 'cancel': if ok is True:
alert.dismiss()
elif mode == 'ok':
alert.accept() alert.accept()
elif ok is False:
alert.dismiss()
return text return res_text
class ToFrame(object): class ToFrame(object):

View File

@ -130,7 +130,7 @@ ele.input('D:\\test1.txt\nD:\\test2.txt')
参数: 参数:
- insure_clear:是否确保清空。为`True`则用`input()`确保值变成`''`,为`False`则用 selenium 元素`clear()`方法 - insure是否确保清空。为`True`则用`input()`确保值变成`''`,为`False`则用 selenium 元素`clear()`方法
返回:`bool`,是否清空成功,不能清空的元素返回`None` 返回:`bool`,是否清空成功,不能清空的元素返回`None`
@ -275,7 +275,7 @@ ele.submit()
- speed拖动的速度传入 0 即瞬间到达 - speed拖动的速度传入 0 即瞬间到达
- shake是否随机抖动 - shake是否随机抖动
返回:`bool`,表示是否拖动成功 返回:None
```python ```python
# 拖动当前元素到距离 50*50 的位置,速度为 100不随机抖动 # 拖动当前元素到距离 50*50 的位置,速度为 100不随机抖动
@ -292,7 +292,7 @@ ele.drag(50, 50, 100, False)
- speed 拖动的速度,传入 0 即瞬间到达 - speed 拖动的速度,传入 0 即瞬间到达
- shake 是否随机抖动 - shake 是否随机抖动
返回:bool表示是否拖动成功 返回:None
```python ```python
# 把 ele1 拖拽到 ele2 上 # 把 ele1 拖拽到 ele2 上

View File

@ -503,8 +503,8 @@ page.set_window_size(800, 600)
参数: 参数:
- mode`'ok'``'cancel'`,若输入其它值,不会按按钮但依然返回文本值 - ok`True`表示确认,`False`表示取消,其它值不会按按钮但依然返回文本值
- text:处理 prompt 提示框时可输入文本 - send:处理 prompt 提示框时可输入文本
- timeout等待提示框出现的超时时间 - timeout等待提示框出现的超时时间
返回:提示框内容文本,未等到提示框则返回`None` 返回:提示框内容文本,未等到提示框则返回`None`
@ -514,10 +514,10 @@ page.set_window_size(800, 600)
txt = page.process_alert() txt = page.process_alert()
# 点击取消 # 点击取消
page.process_alert('cancel') page.process_alert(False)
# 给 prompt 提示框输入文本并点击确定 # 给 prompt 提示框输入文本并点击确定
paeg.process_alert('ok', 'some text') paeg.process_alert(True, 'some text')
# 不处理提示框,只获取提示框文本 # 不处理提示框,只获取提示框文本
txt = page.process_alert(None) txt = page.process_alert(None)

View File

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