mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-11-29 18:47:34 +08:00
1.11.7 点击支持超时时间;处理alert支持超时时间;兼容不同系统的路径分隔符
This commit is contained in:
parent
79e0110956
commit
aebbf4676a
@ -4,9 +4,10 @@
|
||||
@Contact : g1879@qq.com
|
||||
@File : driver_element.py
|
||||
"""
|
||||
from os import sep
|
||||
from pathlib import Path
|
||||
from re import sub
|
||||
from time import sleep, time
|
||||
from time import time
|
||||
from typing import Union, List, Any, Tuple
|
||||
|
||||
from selenium.common.exceptions import TimeoutException, JavascriptException, InvalidElementStateException
|
||||
@ -264,19 +265,23 @@ class DriverElement(DrissionElement):
|
||||
|
||||
return None if r == 'none' else r
|
||||
|
||||
def click(self, by_js: bool = None) -> bool:
|
||||
def click(self, by_js: bool = None, timeout: float = None) -> bool:
|
||||
"""点击元素 \n
|
||||
尝试点击3次,若都失败就改用js点击 \n
|
||||
尝试点击直到超时,若都失败就改用js点击 \n
|
||||
:param by_js: 是否用js点击,为True时直接用js点击,为False时重试失败也不会改用js
|
||||
:param timeout: 尝试点击的超时时间,不指定则使用父页面的超时时间
|
||||
:return: 是否点击成功
|
||||
"""
|
||||
if not by_js:
|
||||
for _ in range(3):
|
||||
timeout = timeout if timeout is not None else self.page.timeout
|
||||
from time import perf_counter
|
||||
t1 = perf_counter()
|
||||
while perf_counter() - t1 <= timeout:
|
||||
try:
|
||||
self.inner_ele.click()
|
||||
return True
|
||||
break
|
||||
except:
|
||||
sleep(0.2)
|
||||
pass
|
||||
|
||||
# 若点击失败,用js方式点击
|
||||
if by_js is not False:
|
||||
@ -414,7 +419,7 @@ class DriverElement(DrissionElement):
|
||||
while not self.run_script(js):
|
||||
pass
|
||||
|
||||
img_path = f'{path}\\{name}'
|
||||
img_path = f'{path}{sep}{name}'
|
||||
self.inner_ele.screenshot(img_path)
|
||||
|
||||
return img_path
|
||||
@ -842,7 +847,7 @@ def _wait_ele(page_or_ele,
|
||||
if mode.lower() not in ('del', 'display', 'hidden'):
|
||||
raise ValueError('Argument mode can only be "del", "display", "hidden"')
|
||||
|
||||
if isinstance(page_or_ele, DrissionElement):
|
||||
if isinstance(page_or_ele, DrissionElement): # TODO: 是否要改为 BaseElement
|
||||
page = page_or_ele.page
|
||||
ele_or_driver = page_or_ele.inner_ele
|
||||
else:
|
||||
|
@ -5,6 +5,7 @@
|
||||
@File : driver_page.py
|
||||
"""
|
||||
from glob import glob
|
||||
from os import sep
|
||||
from pathlib import Path
|
||||
from time import sleep
|
||||
from typing import Union, List, Any, Tuple
|
||||
@ -372,7 +373,7 @@ class DriverPage(BasePage):
|
||||
path = Path(path).absolute()
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
name = get_available_file_name(str(path), f'{name}.png')
|
||||
img_path = f'{path}\\{name}'
|
||||
img_path = f'{path}{sep}{name}'
|
||||
self.driver.save_screenshot(img_path)
|
||||
return img_path
|
||||
|
||||
@ -456,17 +457,27 @@ class DriverPage(BasePage):
|
||||
:param download_path: 下载文件夹路径
|
||||
:return: 文件列表
|
||||
"""
|
||||
return glob(f'{download_path}\\*.crdownload')
|
||||
return glob(f'{download_path}{sep}*.crdownload')
|
||||
|
||||
def process_alert(self, mode: str = 'ok', text: str = None) -> Union[str, None]:
|
||||
def process_alert(self, mode: str = 'ok', text: str = None, timeout: float = None) -> Union[str, None]:
|
||||
"""处理提示框 \n
|
||||
:param mode: 'ok' 或 'cancel',若输入其它值,不会按按钮但依然返回文本值
|
||||
:param text: 处理prompt提示框时可输入文本
|
||||
:param timeout: 等待提示框出现的超时时间
|
||||
:return: 提示框内容文本
|
||||
"""
|
||||
try:
|
||||
alert = self.driver.switch_to.alert
|
||||
except NoAlertPresentException:
|
||||
timeout = timeout if timeout is not None else self.timeout
|
||||
from time import perf_counter
|
||||
alert = None
|
||||
t1 = perf_counter()
|
||||
while perf_counter() - t1 <= timeout:
|
||||
try:
|
||||
alert = self.driver.switch_to.alert
|
||||
break
|
||||
except NoAlertPresentException:
|
||||
pass
|
||||
|
||||
if not alert:
|
||||
return None
|
||||
|
||||
if text:
|
||||
@ -476,7 +487,6 @@ class DriverPage(BasePage):
|
||||
|
||||
if mode == 'cancel':
|
||||
alert.dismiss()
|
||||
|
||||
elif mode == 'ok':
|
||||
alert.accept()
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
@Contact : g1879@qq.com
|
||||
@File : session_page.py
|
||||
"""
|
||||
from os import path as os_PATH
|
||||
from os import path as os_PATH, sep
|
||||
from pathlib import Path
|
||||
from random import randint
|
||||
from re import search as re_SEARCH, sub
|
||||
@ -275,9 +275,9 @@ class SessionPage(BasePage):
|
||||
:param kwargs: 连接参数
|
||||
:return: 下载是否成功(bool)和状态信息(成功时信息为文件路径)的元组
|
||||
"""
|
||||
if file_exists == 'skip' and Path(f'{goal_path}\\{rename}').exists():
|
||||
if file_exists == 'skip' and Path(f'{goal_path}{sep}{rename}').exists():
|
||||
if show_msg:
|
||||
print(f'{file_url}\n{goal_path}\\{rename}\nSkipped.\n')
|
||||
print(f'{file_url}\n{goal_path}{sep}{rename}\nSkipped.\n')
|
||||
|
||||
return False, 'Skipped because a file with the same name already exists.'
|
||||
|
||||
@ -361,12 +361,12 @@ class SessionPage(BasePage):
|
||||
|
||||
goal_Path = Path(goal).absolute()
|
||||
goal_Path.mkdir(parents=True, exist_ok=True)
|
||||
full_path = Path(f'{goal}\\{full_name}')
|
||||
full_path = Path(f'{goal}{sep}{full_name}')
|
||||
|
||||
if full_path.exists():
|
||||
if file_exists == 'rename':
|
||||
full_name = get_available_file_name(goal, full_name)
|
||||
full_path = Path(f'{goal}\\{full_name}')
|
||||
full_path = Path(f'{goal}{sep}{full_name}')
|
||||
|
||||
elif exists == 'skip':
|
||||
skip = True
|
||||
@ -436,7 +436,7 @@ class SessionPage(BasePage):
|
||||
if msg:
|
||||
print(info, '\n')
|
||||
|
||||
info = f'{goal}\\{full_name}' if download_status else info
|
||||
info = f'{goal}{sep}{full_name}' if download_status else info
|
||||
return download_status, info
|
||||
|
||||
retry_times = retry or self.retry_times
|
||||
|
@ -159,6 +159,8 @@ shadow_element = webdriver.execute_script('return arguments[0].shadowRoot', elem
|
||||
|
||||
# 使用 DrissionPage:
|
||||
shadow_element = element.shadow_root
|
||||
# 或
|
||||
shadow_element = element.sr
|
||||
```
|
||||
|
||||
- 用 xpath 直接获取属性或文本节点(返回文本)
|
||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
||||
|
||||
setup(
|
||||
name="DrissionPage",
|
||||
version="1.11.6",
|
||||
version="1.11.7",
|
||||
author="g1879",
|
||||
author_email="g1879@qq.com",
|
||||
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
||||
|
Loading…
Reference in New Issue
Block a user