python-playwright-helper 0.2.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of python-playwright-helper might be problematic. Click here for more details.
- playwright_helper/__init__.py +11 -0
- playwright_helper/libs/__init__.py +11 -0
- playwright_helper/libs/base_po.py +137 -0
- playwright_helper/libs/browser_pool.py +72 -0
- playwright_helper/libs/executor.py +243 -0
- playwright_helper/middlewares/__init__.py +11 -0
- playwright_helper/middlewares/stealth.py +125 -0
- playwright_helper/utils/__init__.py +11 -0
- playwright_helper/utils/browser_utils.py +24 -0
- playwright_helper/utils/file_handle.py +33 -0
- playwright_helper/utils/log_utils.py +15 -0
- playwright_helper/utils/po_utils.py +113 -0
- playwright_helper/utils/template_data.py +5190 -0
- playwright_helper/utils/type_utils.py +129 -0
- python_playwright_helper-0.2.8.dist-info/METADATA +247 -0
- python_playwright_helper-0.2.8.dist-info/RECORD +19 -0
- python_playwright_helper-0.2.8.dist-info/WHEEL +5 -0
- python_playwright_helper-0.2.8.dist-info/licenses/LICENSE +201 -0
- python_playwright_helper-0.2.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
# ---------------------------------------------------------------------------------------------------------
|
|
4
|
+
# ProjectName: playwright-helper
|
|
5
|
+
# FileName: log_utils.py
|
|
6
|
+
# Description: 日志工具模块
|
|
7
|
+
# Author: ASUS
|
|
8
|
+
# CreateDate: 2025/12/13
|
|
9
|
+
# Copyright ©2011-2025. Hunan xxxxxxx Company limited. All rights reserved.
|
|
10
|
+
# ---------------------------------------------------------------------------------------------------------
|
|
11
|
+
"""
|
|
12
|
+
import logging
|
|
13
|
+
|
|
14
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
15
|
+
logger = logging.getLogger("playwright")
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
# ---------------------------------------------------------------------------------------------------------
|
|
4
|
+
# ProjectName: playwright-helper
|
|
5
|
+
# FileName: po_utils.py
|
|
6
|
+
# Description: po对象处理工具
|
|
7
|
+
# Author: ASUS
|
|
8
|
+
# CreateDate: 2025/12/08
|
|
9
|
+
# Copyright ©2011-2025. Hunan xxxxxxx Company limited. All rights reserved.
|
|
10
|
+
# ---------------------------------------------------------------------------------------------------------
|
|
11
|
+
"""
|
|
12
|
+
from typing import Literal, Union, Tuple
|
|
13
|
+
from playwright.async_api import Page, Locator
|
|
14
|
+
|
|
15
|
+
MouseButton = Literal["left", "middle", "right"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def on_click_locator(locator: Locator, button: MouseButton = "left") -> bool:
|
|
19
|
+
try:
|
|
20
|
+
await locator.click(button=button)
|
|
21
|
+
return True
|
|
22
|
+
except (Exception,):
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def on_click_element(page: Page, selector: str, button: MouseButton = "left") -> bool:
|
|
27
|
+
try:
|
|
28
|
+
await page.locator(selector).click(button=button)
|
|
29
|
+
return True
|
|
30
|
+
except (Exception,):
|
|
31
|
+
return False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
async def locator_input_element(locator: Locator, text: str) -> bool:
|
|
35
|
+
"""
|
|
36
|
+
输入内容,输入前会自动清空原来的内容
|
|
37
|
+
:param locator: Locator对象
|
|
38
|
+
:param text: 输入的内容
|
|
39
|
+
:return:
|
|
40
|
+
"""
|
|
41
|
+
try:
|
|
42
|
+
# 填入文字,会清空原内容
|
|
43
|
+
if isinstance(text, str) is False:
|
|
44
|
+
text = str(text)
|
|
45
|
+
await locator.fill(text)
|
|
46
|
+
return True
|
|
47
|
+
except (Exception,):
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async def simulation_input_element(locator: Locator, text: str, delay: int = 100) -> bool:
|
|
52
|
+
"""
|
|
53
|
+
模拟逐字符输入(可用于触发 JS 事件)
|
|
54
|
+
:param locator: 元素定位器对象
|
|
55
|
+
:param text: 输入的内容
|
|
56
|
+
:param int delay: 每个字符延迟输入 100ms
|
|
57
|
+
:return:
|
|
58
|
+
"""
|
|
59
|
+
try:
|
|
60
|
+
if isinstance(text, str) is False:
|
|
61
|
+
text = str(text)
|
|
62
|
+
await locator.type(text, delay=delay)
|
|
63
|
+
return True
|
|
64
|
+
except (Exception,):
|
|
65
|
+
return False
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
async def page_screenshot(page: Page, file_name: str = None, is_full_page: bool = False) -> Union[bytes, None]:
|
|
69
|
+
"""
|
|
70
|
+
保存截图,默认截图整个可见页面(不是整个滚动区域)
|
|
71
|
+
:param page:
|
|
72
|
+
:param file_name: 文件名,若没传递,这返回bytes
|
|
73
|
+
:param is_full_page: true---> 截图整个网页(包括滚动区域)
|
|
74
|
+
:return:
|
|
75
|
+
"""
|
|
76
|
+
if is_full_page is True:
|
|
77
|
+
if file_name is None:
|
|
78
|
+
return await page.screenshot()
|
|
79
|
+
await page.screenshot(path=file_name, full_page=True)
|
|
80
|
+
else:
|
|
81
|
+
await page.screenshot(path=file_name)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
async def locator_element_screenshot(locator: Locator, file_name: str = None) -> bytes:
|
|
85
|
+
"""
|
|
86
|
+
元素截图
|
|
87
|
+
:param locator: Locator对象
|
|
88
|
+
:param file_name: 文件名,若没传递,这返回bytes
|
|
89
|
+
:return:
|
|
90
|
+
"""
|
|
91
|
+
if file_name is None:
|
|
92
|
+
return await locator.screenshot(path=file_name)
|
|
93
|
+
|
|
94
|
+
else:
|
|
95
|
+
await locator.screenshot(path=file_name)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def is_exists_value(element: Locator) -> Tuple[bool, Union[str, None]]:
|
|
99
|
+
"""
|
|
100
|
+
判断一个元素是否存在值
|
|
101
|
+
:param element:
|
|
102
|
+
:return:
|
|
103
|
+
"""
|
|
104
|
+
value = await element.input_value()
|
|
105
|
+
# value = await element.get_attribute("value")
|
|
106
|
+
if value:
|
|
107
|
+
value = value.strip()
|
|
108
|
+
if len(value) > 0:
|
|
109
|
+
return True, value
|
|
110
|
+
else:
|
|
111
|
+
return False, None
|
|
112
|
+
else:
|
|
113
|
+
return False, None
|