selmate 1.0.0__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.
selmate/utils.py ADDED
@@ -0,0 +1,165 @@
1
+ import math
2
+ import random
3
+ import re
4
+ from typing import Tuple, List, Optional
5
+ from urllib.parse import urlparse, urlunparse, urljoin
6
+
7
+ from rapidfuzz import fuzz
8
+
9
+ from selmate.constants import WHITESPACE_PATTERN, CONFIRMATIONS
10
+
11
+
12
+ def normalize_url(href, base_url, save_query=True, save_fragment=False, save_params=False):
13
+ """Normalizes a URL relative to a base URL.
14
+ :param href: The URL to normalize.
15
+ :param base_url: The base URL for relative URLs.
16
+ :param save_query: Whether to keep the query string.
17
+ :param save_fragment: Whether to keep the fragment.
18
+ :param save_params: Whether to keep URL parameters.
19
+ :return: The normalized URL or None if invalid.
20
+ """
21
+ href = href.strip()
22
+ if not href:
23
+ return None
24
+
25
+ parsed_base = urlparse(base_url)
26
+ if not parsed_base.scheme:
27
+ base_url = 'https://' + base_url.lstrip('/')
28
+ parsed_base = urlparse(base_url)
29
+
30
+ parsed_href = urlparse(href)
31
+ if parsed_href.scheme:
32
+ absolute_url = href
33
+ else:
34
+ if href.startswith('//'):
35
+ href = parsed_base.scheme + ':' + href
36
+ absolute_url = urljoin(base_url, href)
37
+
38
+ parsed_url = urlparse(absolute_url)
39
+ if not parsed_url.scheme or not parsed_url.netloc:
40
+ return None
41
+
42
+ cleaned_url = urlunparse((
43
+ parsed_url.scheme,
44
+ parsed_url.netloc,
45
+ parsed_url.path,
46
+ parsed_url.params if save_params else '',
47
+ parsed_url.query if save_query else '',
48
+ parsed_url.fragment if save_fragment else ''
49
+ ))
50
+
51
+ return cleaned_url
52
+
53
+
54
+ def is_webpage(url):
55
+ """Checks if a URL points to a webpage.
56
+ :param url: The URL to check.
57
+ :return: True if the URL is a webpage, False otherwise.
58
+ """
59
+ webpage_extensions = (
60
+ '.html', '.htm', '.php', '.asp', '.aspx', '.jsp', '.jspx',
61
+ '.shtml', '.cfm', '.pl', '.py', '.erb', '.rhtml', '.do',
62
+ '.action', '.cshtml', '.vbhtml', '.phtml', '.cfc', '.ghtml'
63
+ )
64
+
65
+ parsed_url = urlparse(url)
66
+ path = parsed_url.path.lower()
67
+
68
+ if not path or path.endswith('/'):
69
+ return True
70
+
71
+ if path.endswith(webpage_extensions):
72
+ return True
73
+
74
+ last_segment = path.split('/')[-1]
75
+ if '.' in last_segment:
76
+ return False
77
+
78
+ return True
79
+
80
+
81
+ def is_confirmation_text(text, threshold=0.75) -> Optional[float]:
82
+ """Determines if text resembles a confirmation action.
83
+ :param text: The text to check.
84
+ :param threshold: Similarity threshold for confirmation.
85
+ :return: Similarity score if above threshold, None otherwise.
86
+ """
87
+ max_sim = None
88
+ for confirmation in CONFIRMATIONS:
89
+ sim = fuzz.ratio(confirmation, text, processor=norm_string) / 100
90
+ if sim < threshold:
91
+ continue
92
+
93
+ if max_sim is None or sim > max_sim:
94
+ max_sim = sim
95
+
96
+ return max_sim
97
+
98
+
99
+ def latency_time(min_time, max_time):
100
+ """Generates a random latency time within a range.
101
+ :param min_time: Minimum latency time.
102
+ :param max_time: Maximum latency time.
103
+ :return: Random latency time.
104
+ """
105
+ return random.uniform(min_time, max_time)
106
+
107
+
108
+ def norm_string(s):
109
+ """Normalizes a string by stripping and lowercasing.
110
+ :param s: The string to normalize.
111
+ :return: Normalized string.
112
+ """
113
+ s = s.strip().lower()
114
+ return re.sub(WHITESPACE_PATTERN, ' ', s)
115
+
116
+
117
+ def generate_parabolic_path(x1, y1, x2, y2, step_length=10.0) -> List[Tuple[float, float]]:
118
+ """Generates a parabolic path between two points.
119
+ :param x1: Starting x-coordinate.
120
+ :param y1: Starting y-coordinate.
121
+ :param x2: Ending x-coordinate.
122
+ :param y2: Ending y-coordinate.
123
+ :param step_length: Length of each step.
124
+ :return: List of (x, y) coordinates.
125
+ """
126
+ dist = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) * math.pi / 1.5
127
+ steps = math.ceil(dist / step_length)
128
+ path = []
129
+ for i in range(steps + 1):
130
+ t = i / steps
131
+ x = x1 + (x2 - x1) * t
132
+ y_offset = steps * 4 * t * (1 - t)
133
+ y = y1 + (y2 - y1) * t + y_offset
134
+ path.append((x, y))
135
+ return path
136
+
137
+
138
+ def generate_curved_path(x1, y1, x2, y2, step_length=10.0):
139
+ """Generates a curved path between two points.
140
+ :param x1: Starting x-coordinate.
141
+ :param y1: Starting y-coordinate.
142
+ :param x2: Ending x-coordinate.
143
+ :param y2: Ending y-coordinate.
144
+ :param step_length: Length of each step.
145
+ :return: List of (x, y) coordinates.
146
+ """
147
+ points = [(x1, y1)]
148
+
149
+ dx = x2 - x1
150
+ total_dist = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
151
+
152
+ num_steps = max(1, int(total_dist / step_length))
153
+ height = abs(y2 - y1) * 0.5 + 0.5
154
+
155
+ for i in range(1, num_steps):
156
+ t = i / num_steps
157
+ x = x1 + t * dx
158
+ arc = height * (1 - ((2 * t - 1) ** 2))
159
+ y = y1 + t * (y2 - y1) + arc
160
+
161
+ points.append((x, y))
162
+
163
+ points.append((x2, y2))
164
+
165
+ return points
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 FINWAX
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.3
2
+ Name: selmate
3
+ Version: 1.0.0
4
+ Summary:
5
+ License: MIT License
6
+
7
+ Copyright (c) 2025 FINWAX
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ Author: FINWAX
27
+ Author-email: waxbid@gmail.com
28
+ Requires-Python: >=3.10
29
+ Classifier: License :: Other/Proprietary License
30
+ Classifier: Programming Language :: Python :: 3
31
+ Classifier: Programming Language :: Python :: 3.10
32
+ Classifier: Programming Language :: Python :: 3.11
33
+ Classifier: Programming Language :: Python :: 3.12
34
+ Classifier: Programming Language :: Python :: 3.13
35
+ Requires-Dist: beautifulsoup4 (>=4.13.4,<5.0.0)
36
+ Requires-Dist: lxml (>=5.4.0,<6.0.0)
37
+ Requires-Dist: rapidfuzz (>=3.13.0,<4.0.0)
38
+ Requires-Dist: selenium (>=4.33.0,<5.0.0)
39
+ Description-Content-Type: text/markdown
40
+
41
+ # Selmate
42
+
43
+ Selmate is a Python utility library designed to enhance Selenium WebDriver automation by providing human-like
44
+ interactions, robust exception handling, and utilities for common web automation tasks. It simplifies interactions with
45
+ web elements, handles popups, normalizes URLs, and simulates natural user behavior like mouse movements and scrolling.
46
+
47
+ ## Features
48
+
49
+ - **Safe Element Interactions**: Wrappers for Selenium operations (clicks, scrolls, etc.) with built-in exception
50
+ handling for stale elements, timeouts, and more.
51
+ - **Human-Like Behavior**: Simulates realistic mouse movements, scrolling, and latency to mimic human interactions.
52
+ - **Popup Handling**: Automatically detects and handles popup banners, including those within iframes, with options to
53
+ accept or close them.
54
+ - **URL Normalization**: Utilities to normalize URLs relative to a base URL, with configurable handling of query
55
+ strings, fragments, and parameters.
56
+ - **JavaScript Integration**: Execute JavaScript for advanced interactions like smooth scrolling, element removal, and
57
+ visibility checks.
58
+ - **Text Similarity**: Identify confirmation buttons or close buttons using fuzzy text matching.
59
+
60
+ ## Installation
61
+
62
+ Install Selmate via pip:
63
+
64
+ ```bash
65
+ pip install selmate
66
+ ```
67
+
68
+ ## Usage
69
+
70
+ Here are some examples of using Selmate's core functionalities:
71
+
72
+ ### Example 1: Safe Element Click
73
+
74
+ ```python
75
+ from selenium import webdriver
76
+ from selmate.composites import complex_click
77
+ from selmate.selenium_primitives import find_element_safely
78
+ from selenium.webdriver.common.by import By
79
+
80
+ driver = webdriver.Chrome()
81
+ driver.get("https://example.com")
82
+
83
+ # Safely find and click a button
84
+ button = find_element_safely(By.ID, "submit-button", driver)
85
+ if button and complex_click(button, driver):
86
+ print("Button clicked successfully")
87
+
88
+ driver.quit()
89
+ ```
90
+
91
+ ### Example 2: Handling Popup Banners
92
+
93
+ ```python
94
+ from selenium import webdriver
95
+ from selmate.composites import bypass_popup_banners
96
+
97
+ driver = webdriver.Chrome()
98
+ driver.get("https://example.com")
99
+
100
+ # Automatically handle popup banners
101
+ bypass_popup_banners(driver, observation_capacity=50, success_capacity=3, try_close=True)
102
+ print("Popups handled")
103
+
104
+ driver.quit()
105
+ ```
106
+
107
+ ### Example 3: Human-Like Mouse Movement
108
+
109
+ ```python
110
+ from selenium import webdriver
111
+ from selmate.composites import wander_between_2_elements
112
+ from selmate.selenium_primitives import find_element_safely
113
+ from selenium.webdriver.common.by import By
114
+
115
+ driver = webdriver.Chrome()
116
+ driver.get("https://example.com")
117
+
118
+ # Find two elements and simulate mouse movement between them
119
+ element1 = find_element_safely(By.ID, "element1", driver)
120
+ element2 = find_element_safely(By.ID, "element2", driver)
121
+ if element1 and element2:
122
+ wander_between_2_elements(element1, element2, driver)
123
+
124
+ driver.quit()
125
+ ```
126
+
127
+ ## License
128
+
129
+ Selmate is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
130
+
131
+ ## Contact
132
+
133
+ For questions or support, open an issue or contact the maintainer at waxbid@gmail.com.
@@ -0,0 +1,13 @@
1
+ selmate/__init__.py,sha256=ndpiwSMdcqh1TZO1pEdZ-pIzngdSoa-cXIhHeJxAeRw,79
2
+ selmate/composites.py,sha256=gIgFTu0TGExmlS0ptN-NRYPhizf8O8ROZSFkkL3yi8U,25442
3
+ selmate/constants.py,sha256=8ZeIZ0v_lWx7_-lN0rYlTNnOCj2zMih1ePQVgtKUsp4,6978
4
+ selmate/humanity/constants.py,sha256=YYRtMlLapqm1DBCuhdJHFH1Hq4bDSgeXMcpbX-uedoA,234
5
+ selmate/humanity/latency.py,sha256=xJ2JWtmgxmkqDsTvCMihmuM3KKVP8Crf2YaH2LP7qEs,1263
6
+ selmate/js_primitives.py,sha256=4-etOV4ftYwPE9hAOkyS84W19mjzcjYE7enpwdeESEo,6158
7
+ selmate/safe_exceptions.py,sha256=L2RPD6TSHCIMsyYxiSHbS0EzCaXsyavUi4NvZZqXf7w,1425
8
+ selmate/selenium_primitives.py,sha256=hKYPFtQCOSuNt3_S1U2Ce9sDhtFe5bhvHwgMChRuUdE,2667
9
+ selmate/utils.py,sha256=NWImi_DuM9JmsN1E8br2YjCMaWte-mdFbWAPuFHObsY,5012
10
+ selmate-1.0.0.dist-info/LICENSE,sha256=7Ivo3S2GrdGr_wc98qnPyERQMV4wxZULNMzgk09S758,1082
11
+ selmate-1.0.0.dist-info/METADATA,sha256=yn7YjRHTIcZANF1cEetSBwzZ0OowU6y6gLXmyoNbHvY,4878
12
+ selmate-1.0.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
13
+ selmate-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.1.3
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any