strawdriver 0.1.0__tar.gz
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.
- strawdriver-0.1.0/PKG-INFO +79 -0
- strawdriver-0.1.0/README.md +56 -0
- strawdriver-0.1.0/pyproject.toml +38 -0
- strawdriver-0.1.0/setup.cfg +4 -0
- strawdriver-0.1.0/strawdriver/__init__.py +12 -0
- strawdriver-0.1.0/strawdriver/driver.py +435 -0
- strawdriver-0.1.0/strawdriver/element.py +150 -0
- strawdriver-0.1.0/strawdriver/physics.py +83 -0
- strawdriver-0.1.0/strawdriver/py.typed +0 -0
- strawdriver-0.1.0/strawdriver/stealth.py +157 -0
- strawdriver-0.1.0/strawdriver.egg-info/PKG-INFO +79 -0
- strawdriver-0.1.0/strawdriver.egg-info/SOURCES.txt +14 -0
- strawdriver-0.1.0/strawdriver.egg-info/dependency_links.txt +1 -0
- strawdriver-0.1.0/strawdriver.egg-info/requires.txt +1 -0
- strawdriver-0.1.0/strawdriver.egg-info/top_level.txt +1 -0
- strawdriver-0.1.0/tests/test_driver.py +41 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: strawdriver
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stealth, high-performance Chromium automation driver over Chrome DevTools Protocol (CDP).
|
|
5
|
+
Author: StrawDriver Contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/strawdriver/strawdriver
|
|
8
|
+
Keywords: automation,chromium,cdp,chrome-devtools-protocol,stealth,webdriver
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: websockets>=12.0
|
|
23
|
+
|
|
24
|
+
# StrawDriver
|
|
25
|
+
|
|
26
|
+
A standalone, stealth Chromium automation driver communicating directly via the Chrome DevTools Protocol (CDP) over WebSockets.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Architecture
|
|
31
|
+
|
|
32
|
+
- **`strawdriver/driver.py`**: Core `StrawDriver` client managing browser lifecycle, tabs, cookies, and CDP transport.
|
|
33
|
+
- **`strawdriver/element.py`**: `Element` abstraction for querying, scrolling, clicking, and filling fields.
|
|
34
|
+
- **`strawdriver/physics.py`**: Bézier curve trajectory generator and natural typing cadence.
|
|
35
|
+
- **`strawdriver/stealth.py`**: Pre-document injections to mask automation indicators and normalize browser properties.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install locally in editable mode:
|
|
42
|
+
|
|
43
|
+
```powershell
|
|
44
|
+
cd "C:\Users\Admin\Desktop\VaultO'AllMighty\webdriver"
|
|
45
|
+
pip install -e .
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Quickstart
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import asyncio
|
|
54
|
+
from strawdriver import StrawDriver
|
|
55
|
+
|
|
56
|
+
async def main():
|
|
57
|
+
driver = StrawDriver(headless=False)
|
|
58
|
+
await driver.start()
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
await driver.goto("https://example.com", wait_until_network_idle=True)
|
|
62
|
+
element = await driver.wait_for_selector("a")
|
|
63
|
+
print("Link text:", await element.text())
|
|
64
|
+
finally:
|
|
65
|
+
await driver.close()
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
asyncio.run(main())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Running Tests
|
|
74
|
+
|
|
75
|
+
Execute the unit test suite:
|
|
76
|
+
|
|
77
|
+
```powershell
|
|
78
|
+
python -m unittest discover -s tests
|
|
79
|
+
```
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# StrawDriver
|
|
2
|
+
|
|
3
|
+
A standalone, stealth Chromium automation driver communicating directly via the Chrome DevTools Protocol (CDP) over WebSockets.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Architecture
|
|
8
|
+
|
|
9
|
+
- **`strawdriver/driver.py`**: Core `StrawDriver` client managing browser lifecycle, tabs, cookies, and CDP transport.
|
|
10
|
+
- **`strawdriver/element.py`**: `Element` abstraction for querying, scrolling, clicking, and filling fields.
|
|
11
|
+
- **`strawdriver/physics.py`**: Bézier curve trajectory generator and natural typing cadence.
|
|
12
|
+
- **`strawdriver/stealth.py`**: Pre-document injections to mask automation indicators and normalize browser properties.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Install locally in editable mode:
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
cd "C:\Users\Admin\Desktop\VaultO'AllMighty\webdriver"
|
|
22
|
+
pip install -e .
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import asyncio
|
|
31
|
+
from strawdriver import StrawDriver
|
|
32
|
+
|
|
33
|
+
async def main():
|
|
34
|
+
driver = StrawDriver(headless=False)
|
|
35
|
+
await driver.start()
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
await driver.goto("https://example.com", wait_until_network_idle=True)
|
|
39
|
+
element = await driver.wait_for_selector("a")
|
|
40
|
+
print("Link text:", await element.text())
|
|
41
|
+
finally:
|
|
42
|
+
await driver.close()
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
asyncio.run(main())
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Running Tests
|
|
51
|
+
|
|
52
|
+
Execute the unit test suite:
|
|
53
|
+
|
|
54
|
+
```powershell
|
|
55
|
+
python -m unittest discover -s tests
|
|
56
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "strawdriver"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Stealth, high-performance Chromium automation driver over Chrome DevTools Protocol (CDP)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "StrawDriver Contributors" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["automation", "chromium", "cdp", "chrome-devtools-protocol", "stealth", "webdriver"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Software Development :: Testing",
|
|
27
|
+
"Topic :: Internet :: WWW/HTTP :: Browsers",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"websockets>=12.0",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/strawdriver/strawdriver"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["."]
|
|
38
|
+
include = ["strawdriver*"]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
StrawDriver
|
|
3
|
+
~~~~~~~~~~~
|
|
4
|
+
A modern, lightweight, stealth Chromium automation driver built directly
|
|
5
|
+
on top of the Chrome DevTools Protocol (CDP) over WebSockets.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .driver import StrawDriver
|
|
9
|
+
from .element import Element
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
__all__ = ["StrawDriver", "Element"]
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import base64
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import random
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
import tempfile
|
|
9
|
+
import urllib.parse
|
|
10
|
+
import urllib.request
|
|
11
|
+
from typing import Any, Callable, Dict, List, Optional
|
|
12
|
+
import websockets
|
|
13
|
+
|
|
14
|
+
from .element import Element
|
|
15
|
+
from .physics import generate_bezier_trajectory, human_keystroke_cadence
|
|
16
|
+
from .stealth import HARDENED_STEALTH_SCRIPT
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class StrawDriver:
|
|
20
|
+
"""
|
|
21
|
+
StrawDriver: A lightweight, stealth Chromium automation driver built directly
|
|
22
|
+
on the Chrome DevTools Protocol (CDP) over WebSockets.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
executable_path: Optional[str] = None,
|
|
28
|
+
port: int = 9222,
|
|
29
|
+
headless: bool = False,
|
|
30
|
+
user_data_dir: Optional[str] = None,
|
|
31
|
+
window_size: str = "1920,1080",
|
|
32
|
+
args: Optional[List[str]] = None,
|
|
33
|
+
):
|
|
34
|
+
self.executable_path = executable_path or self._find_chromium()
|
|
35
|
+
self.port = port
|
|
36
|
+
self.headless = headless
|
|
37
|
+
self.window_size = window_size
|
|
38
|
+
self._is_temp_profile = user_data_dir is None
|
|
39
|
+
self.user_data_dir = user_data_dir or tempfile.mkdtemp(prefix="strawdriver_")
|
|
40
|
+
self.args = list(args or [])
|
|
41
|
+
|
|
42
|
+
# Process and WebSocket handles
|
|
43
|
+
self.process: Optional[subprocess.Popen] = None
|
|
44
|
+
self.ws: Any = None
|
|
45
|
+
self._command_id = 0
|
|
46
|
+
self._pending_requests: Dict[int, asyncio.Future] = {}
|
|
47
|
+
self._event_listeners: Dict[str, List[Callable[[Dict[str, Any]], None]]] = {}
|
|
48
|
+
self._receive_task: Optional[asyncio.Task] = None
|
|
49
|
+
|
|
50
|
+
# State tracking
|
|
51
|
+
self.current_target_id: Optional[str] = None
|
|
52
|
+
self.current_mouse_x: float = 100.0
|
|
53
|
+
self.current_mouse_y: float = 100.0
|
|
54
|
+
|
|
55
|
+
# Network idle tracking
|
|
56
|
+
self._inflight_requests = 0
|
|
57
|
+
self._last_network_activity = 0.0
|
|
58
|
+
|
|
59
|
+
def _find_chromium(self) -> str:
|
|
60
|
+
"""Auto-detects Chrome or Edge binaries on Windows."""
|
|
61
|
+
candidate_paths = [
|
|
62
|
+
os.path.expandvars(r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"),
|
|
63
|
+
os.path.expandvars(r"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"),
|
|
64
|
+
os.path.expandvars(r"%LocalAppData%\Google\Chrome\Application\chrome.exe"),
|
|
65
|
+
os.path.expandvars(r"%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"),
|
|
66
|
+
os.path.expandvars(r"%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"),
|
|
67
|
+
]
|
|
68
|
+
for path in candidate_paths:
|
|
69
|
+
if os.path.exists(path):
|
|
70
|
+
return path
|
|
71
|
+
raise FileNotFoundError("Could not locate a valid Chromium binary (Chrome or Edge).")
|
|
72
|
+
|
|
73
|
+
# =========================================================================
|
|
74
|
+
# Lifecycle & Connection Management
|
|
75
|
+
# =========================================================================
|
|
76
|
+
|
|
77
|
+
async def start(self):
|
|
78
|
+
"""Launches Chromium with stealth arguments and connects to active tab."""
|
|
79
|
+
cmd = [
|
|
80
|
+
self.executable_path,
|
|
81
|
+
f"--remote-debugging-port={self.port}",
|
|
82
|
+
f"--user-data-dir={self.user_data_dir}",
|
|
83
|
+
f"--window-size={self.window_size}",
|
|
84
|
+
# Anti-detection flags
|
|
85
|
+
"--disable-blink-features=AutomationControlled",
|
|
86
|
+
"--disable-infobars",
|
|
87
|
+
"--no-first-run",
|
|
88
|
+
"--no-default-browser-check",
|
|
89
|
+
"--disable-features=IsolateOrigins,site-per-process",
|
|
90
|
+
"--password-store=basic",
|
|
91
|
+
"--lang=en-US,en",
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
if self.headless:
|
|
95
|
+
cmd.append("--headless=new")
|
|
96
|
+
|
|
97
|
+
if self.args:
|
|
98
|
+
cmd.extend(self.args)
|
|
99
|
+
|
|
100
|
+
cmd.append("about:blank")
|
|
101
|
+
|
|
102
|
+
self.process = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
103
|
+
|
|
104
|
+
# Discover active page target WebSocket
|
|
105
|
+
page_target = await self._discover_initial_page_target()
|
|
106
|
+
self.current_target_id = page_target["id"]
|
|
107
|
+
ws_url = page_target["webSocketDebuggerUrl"]
|
|
108
|
+
|
|
109
|
+
self.ws = await websockets.connect(ws_url, max_size=100 * 1024 * 1024)
|
|
110
|
+
self._receive_task = asyncio.create_task(self._event_loop())
|
|
111
|
+
|
|
112
|
+
# Initialize core domains
|
|
113
|
+
await self._init_page_domains()
|
|
114
|
+
|
|
115
|
+
async def _init_page_domains(self):
|
|
116
|
+
"""Enables essential CDP domains and registers network/stealth hooks."""
|
|
117
|
+
await self.send("Page.enable")
|
|
118
|
+
await self.send("Runtime.enable")
|
|
119
|
+
await self.send("DOM.enable")
|
|
120
|
+
await self.send("Network.enable")
|
|
121
|
+
|
|
122
|
+
# Network activity tracking for wait_for_network_idle
|
|
123
|
+
def on_req_will_be_sent(_p):
|
|
124
|
+
self._inflight_requests += 1
|
|
125
|
+
self._last_network_activity = asyncio.get_running_loop().time()
|
|
126
|
+
|
|
127
|
+
def on_loading_finished(_p):
|
|
128
|
+
self._inflight_requests = max(0, self._inflight_requests - 1)
|
|
129
|
+
self._last_network_activity = asyncio.get_running_loop().time()
|
|
130
|
+
|
|
131
|
+
def on_loading_failed(_p):
|
|
132
|
+
self._inflight_requests = max(0, self._inflight_requests - 1)
|
|
133
|
+
self._last_network_activity = asyncio.get_running_loop().time()
|
|
134
|
+
|
|
135
|
+
self.on("Network.requestWillBeSent", on_req_will_be_sent)
|
|
136
|
+
self.on("Network.loadingFinished", on_loading_finished)
|
|
137
|
+
self.on("Network.loadingFailed", on_loading_failed)
|
|
138
|
+
|
|
139
|
+
# Inject hardened stealth script across all new navigations/iframes
|
|
140
|
+
await self.send(
|
|
141
|
+
"Page.addScriptToEvaluateOnNewDocument",
|
|
142
|
+
{"source": HARDENED_STEALTH_SCRIPT},
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
async def _discover_initial_page_target(self) -> Dict[str, Any]:
|
|
146
|
+
"""Polls /json/list until an active page target is available."""
|
|
147
|
+
url = f"http://127.0.0.1:{self.port}/json/list"
|
|
148
|
+
for _ in range(60):
|
|
149
|
+
try:
|
|
150
|
+
with urllib.request.urlopen(url, timeout=1) as resp:
|
|
151
|
+
targets = json.loads(resp.read().decode())
|
|
152
|
+
for t in targets:
|
|
153
|
+
if t.get("type") == "page" and "webSocketDebuggerUrl" in t:
|
|
154
|
+
return t
|
|
155
|
+
except Exception:
|
|
156
|
+
await asyncio.sleep(0.1)
|
|
157
|
+
raise TimeoutError(f"Could not connect to browser target at port {self.port}.")
|
|
158
|
+
|
|
159
|
+
async def _event_loop(self):
|
|
160
|
+
"""Processes incoming CDP responses and dispatches event listeners."""
|
|
161
|
+
try:
|
|
162
|
+
async for raw in self.ws:
|
|
163
|
+
msg = json.loads(raw)
|
|
164
|
+
if "id" in msg and msg["id"] in self._pending_requests:
|
|
165
|
+
future = self._pending_requests.pop(msg["id"])
|
|
166
|
+
if "error" in msg:
|
|
167
|
+
future.set_exception(RuntimeError(msg["error"].get("message", "CDP Error")))
|
|
168
|
+
else:
|
|
169
|
+
future.set_result(msg.get("result", {}))
|
|
170
|
+
elif "method" in msg:
|
|
171
|
+
method = msg["method"]
|
|
172
|
+
params = msg.get("params", {})
|
|
173
|
+
if method in self._event_listeners:
|
|
174
|
+
for cb in self._event_listeners[method]:
|
|
175
|
+
try:
|
|
176
|
+
cb(params)
|
|
177
|
+
except Exception:
|
|
178
|
+
pass
|
|
179
|
+
except asyncio.CancelledError:
|
|
180
|
+
pass
|
|
181
|
+
|
|
182
|
+
async def send(self, method: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
183
|
+
"""Dispatches a JSON-RPC request to the connected target."""
|
|
184
|
+
self._command_id += 1
|
|
185
|
+
cid = self._command_id
|
|
186
|
+
payload = {"id": cid, "method": method, "params": params or {}}
|
|
187
|
+
future = asyncio.get_running_loop().create_future()
|
|
188
|
+
self._pending_requests[cid] = future
|
|
189
|
+
await self.ws.send(json.dumps(payload))
|
|
190
|
+
return await future
|
|
191
|
+
|
|
192
|
+
def on(self, event_name: str, callback: Callable[[Dict[str, Any]], None]):
|
|
193
|
+
"""Registers a callback for CDP events."""
|
|
194
|
+
self._event_listeners.setdefault(event_name, []).append(callback)
|
|
195
|
+
|
|
196
|
+
# =========================================================================
|
|
197
|
+
# Multi-Tab & Target Routing
|
|
198
|
+
# =========================================================================
|
|
199
|
+
|
|
200
|
+
async def list_tabs(self) -> List[Dict[str, Any]]:
|
|
201
|
+
"""Returns all open page tabs."""
|
|
202
|
+
url = f"http://127.0.0.1:{self.port}/json/list"
|
|
203
|
+
with urllib.request.urlopen(url, timeout=2) as resp:
|
|
204
|
+
targets = json.loads(resp.read().decode())
|
|
205
|
+
return [t for t in targets if t.get("type") == "page"]
|
|
206
|
+
|
|
207
|
+
async def new_tab(self, url: str = "about:blank") -> str:
|
|
208
|
+
"""Opens a new tab, switches StrawDriver focus to it, and returns its target ID."""
|
|
209
|
+
encoded_url = urllib.parse.quote(url)
|
|
210
|
+
endpoint = f"http://127.0.0.1:{self.port}/json/new?{encoded_url}"
|
|
211
|
+
req = urllib.request.Request(endpoint, method="PUT")
|
|
212
|
+
with urllib.request.urlopen(req, timeout=5) as resp:
|
|
213
|
+
target_data = json.loads(resp.read().decode())
|
|
214
|
+
target_id = target_data["id"]
|
|
215
|
+
await self.switch_to_tab(target_id)
|
|
216
|
+
return target_id
|
|
217
|
+
|
|
218
|
+
async def switch_to_tab(self, target_id: str):
|
|
219
|
+
"""Switches the driver connection to another tab by its target ID."""
|
|
220
|
+
tabs = await self.list_tabs()
|
|
221
|
+
match = next((t for t in tabs if t["id"] == target_id), None)
|
|
222
|
+
if not match:
|
|
223
|
+
raise ValueError(f"No tab found with target ID: {target_id}")
|
|
224
|
+
|
|
225
|
+
if self._receive_task:
|
|
226
|
+
self._receive_task.cancel()
|
|
227
|
+
if self.ws:
|
|
228
|
+
await self.ws.close()
|
|
229
|
+
|
|
230
|
+
self._pending_requests.clear()
|
|
231
|
+
self._event_listeners.clear()
|
|
232
|
+
|
|
233
|
+
self.ws = await websockets.connect(match["webSocketDebuggerUrl"], max_size=100 * 1024 * 1024)
|
|
234
|
+
self._receive_task = asyncio.create_task(self._event_loop())
|
|
235
|
+
self.current_target_id = target_id
|
|
236
|
+
await self._init_page_domains()
|
|
237
|
+
|
|
238
|
+
async def close_tab(self, target_id: Optional[str] = None):
|
|
239
|
+
"""Closes a specific tab or the current active tab."""
|
|
240
|
+
tid = target_id or self.current_target_id
|
|
241
|
+
endpoint = f"http://127.0.0.1:{self.port}/json/close/{tid}"
|
|
242
|
+
with urllib.request.urlopen(endpoint, timeout=5) as resp:
|
|
243
|
+
pass
|
|
244
|
+
if tid == self.current_target_id:
|
|
245
|
+
tabs = await self.list_tabs()
|
|
246
|
+
if tabs:
|
|
247
|
+
await self.switch_to_tab(tabs[0]["id"])
|
|
248
|
+
|
|
249
|
+
# =========================================================================
|
|
250
|
+
# Navigation & Synchronization
|
|
251
|
+
# =========================================================================
|
|
252
|
+
|
|
253
|
+
async def goto(self, url: str, wait_until_network_idle: bool = False, timeout: float = 30.0):
|
|
254
|
+
"""Navigates to URL and waits for Page.loadEventFired and optional network idle."""
|
|
255
|
+
loop = asyncio.get_running_loop()
|
|
256
|
+
load_future = loop.create_future()
|
|
257
|
+
|
|
258
|
+
def on_load(_params):
|
|
259
|
+
if not load_future.done():
|
|
260
|
+
load_future.set_result(True)
|
|
261
|
+
|
|
262
|
+
self.on("Page.loadEventFired", on_load)
|
|
263
|
+
await self.send("Page.navigate", {"url": url})
|
|
264
|
+
await asyncio.wait_for(load_future, timeout=timeout)
|
|
265
|
+
|
|
266
|
+
if wait_until_network_idle:
|
|
267
|
+
await self.wait_for_network_idle(idle_time=0.5, timeout=timeout)
|
|
268
|
+
|
|
269
|
+
async def wait_for_network_idle(self, idle_time: float = 0.5, timeout: float = 30.0):
|
|
270
|
+
"""Waits until no network requests have been inflight for `idle_time` seconds."""
|
|
271
|
+
start_time = asyncio.get_running_loop().time()
|
|
272
|
+
while True:
|
|
273
|
+
now = asyncio.get_running_loop().time()
|
|
274
|
+
if now - start_time > timeout:
|
|
275
|
+
raise TimeoutError("Network idle wait timed out.")
|
|
276
|
+
if self._inflight_requests == 0 and (now - self._last_network_activity >= idle_time):
|
|
277
|
+
return
|
|
278
|
+
await asyncio.sleep(0.05)
|
|
279
|
+
|
|
280
|
+
async def evaluate(self, expression: str) -> Any:
|
|
281
|
+
"""Executes JavaScript in the page context and returns the serializable result."""
|
|
282
|
+
res = await self.send("Runtime.evaluate", {
|
|
283
|
+
"expression": expression,
|
|
284
|
+
"returnByValue": True,
|
|
285
|
+
"awaitPromise": True,
|
|
286
|
+
})
|
|
287
|
+
return res.get("result", {}).get("value")
|
|
288
|
+
|
|
289
|
+
# =========================================================================
|
|
290
|
+
# Element Querying & Waiting
|
|
291
|
+
# =========================================================================
|
|
292
|
+
|
|
293
|
+
async def query_selector(self, selector: str) -> Optional[Element]:
|
|
294
|
+
"""Finds a DOM element matching selector or returns None."""
|
|
295
|
+
doc = await self.send("DOM.getDocument")
|
|
296
|
+
root_node_id = doc["root"]["nodeId"]
|
|
297
|
+
node = await self.send("DOM.querySelector", {
|
|
298
|
+
"nodeId": root_node_id,
|
|
299
|
+
"selector": selector
|
|
300
|
+
})
|
|
301
|
+
node_id = node.get("nodeId", 0)
|
|
302
|
+
return Element(self, node_id, selector) if node_id != 0 else None
|
|
303
|
+
|
|
304
|
+
async def wait_for_selector(self, selector: str, timeout: float = 30.0) -> Element:
|
|
305
|
+
"""Polls until an element matching selector appears and is ready."""
|
|
306
|
+
start_time = asyncio.get_running_loop().time()
|
|
307
|
+
while True:
|
|
308
|
+
el = await self.query_selector(selector)
|
|
309
|
+
if el is not None:
|
|
310
|
+
return el
|
|
311
|
+
if asyncio.get_running_loop().time() - start_time > timeout:
|
|
312
|
+
raise TimeoutError(f"Timeout waiting for selector: {selector}")
|
|
313
|
+
await asyncio.sleep(0.1)
|
|
314
|
+
|
|
315
|
+
async def click(self, selector: str, human_curve: bool = True):
|
|
316
|
+
"""Finds element and clicks with realistic mouse movement."""
|
|
317
|
+
el = await self.wait_for_selector(selector)
|
|
318
|
+
await el.click(human_curve=human_curve)
|
|
319
|
+
|
|
320
|
+
async def fill(self, selector: str, text: str):
|
|
321
|
+
"""Focuses element, clears content, and enters text naturally."""
|
|
322
|
+
el = await self.wait_for_selector(selector)
|
|
323
|
+
await el.fill(text)
|
|
324
|
+
|
|
325
|
+
async def get_text(self, selector: str) -> str:
|
|
326
|
+
"""Retrieves innerText of element matching selector."""
|
|
327
|
+
el = await self.wait_for_selector(selector)
|
|
328
|
+
return await el.text()
|
|
329
|
+
|
|
330
|
+
async def get_attribute(self, selector: str, name: str) -> Optional[str]:
|
|
331
|
+
"""Retrieves an attribute value of element matching selector."""
|
|
332
|
+
el = await self.wait_for_selector(selector)
|
|
333
|
+
return await el.attribute(name)
|
|
334
|
+
|
|
335
|
+
# =========================================================================
|
|
336
|
+
# Human Physics & Behavioral Actions
|
|
337
|
+
# =========================================================================
|
|
338
|
+
|
|
339
|
+
async def mouse_move(self, target_x: float, target_y: float, human_curve: bool = True):
|
|
340
|
+
"""Moves cursor to destination coordinates along a human Bezier curve."""
|
|
341
|
+
if human_curve:
|
|
342
|
+
traj = generate_bezier_trajectory(self.current_mouse_x, self.current_mouse_y, target_x, target_y)
|
|
343
|
+
for x, y, delay in traj:
|
|
344
|
+
await self.send("Input.dispatchMouseEvent", {"type": "mouseMoved", "x": x, "y": y})
|
|
345
|
+
self.current_mouse_x = x
|
|
346
|
+
self.current_mouse_y = y
|
|
347
|
+
await asyncio.sleep(delay)
|
|
348
|
+
else:
|
|
349
|
+
await self.send("Input.dispatchMouseEvent", {"type": "mouseMoved", "x": target_x, "y": target_y})
|
|
350
|
+
self.current_mouse_x = target_x
|
|
351
|
+
self.current_mouse_y = target_y
|
|
352
|
+
|
|
353
|
+
async def scroll(self, delta_y: float, steps: int = 10):
|
|
354
|
+
"""Smoothly scrolls the page up or down mimicking human wheel turns."""
|
|
355
|
+
step_delta = delta_y / steps
|
|
356
|
+
for _ in range(steps):
|
|
357
|
+
await self.send("Input.dispatchMouseEvent", {
|
|
358
|
+
"type": "mouseWheel",
|
|
359
|
+
"x": self.current_mouse_x,
|
|
360
|
+
"y": self.current_mouse_y,
|
|
361
|
+
"deltaX": 0,
|
|
362
|
+
"deltaY": step_delta
|
|
363
|
+
})
|
|
364
|
+
await asyncio.sleep(random.uniform(0.02, 0.05))
|
|
365
|
+
|
|
366
|
+
async def type_text(self, text: str, natural_cadence: bool = True):
|
|
367
|
+
"""Types raw text into currently focused element."""
|
|
368
|
+
if natural_cadence:
|
|
369
|
+
cadence = human_keystroke_cadence(text)
|
|
370
|
+
for char, delay in cadence:
|
|
371
|
+
await self.send("Input.dispatchKeyEvent", {"type": "keyDown", "text": char})
|
|
372
|
+
await self.send("Input.dispatchKeyEvent", {"type": "keyUp"})
|
|
373
|
+
await asyncio.sleep(delay)
|
|
374
|
+
else:
|
|
375
|
+
for char in text:
|
|
376
|
+
await self.send("Input.dispatchKeyEvent", {"type": "keyDown", "text": char})
|
|
377
|
+
await self.send("Input.dispatchKeyEvent", {"type": "keyUp"})
|
|
378
|
+
await asyncio.sleep(0.02)
|
|
379
|
+
|
|
380
|
+
# =========================================================================
|
|
381
|
+
# Cookie & Session Storage
|
|
382
|
+
# =========================================================================
|
|
383
|
+
|
|
384
|
+
async def get_cookies(self, urls: Optional[List[str]] = None) -> List[Dict[str, Any]]:
|
|
385
|
+
"""Retrieves cookies for current session or specified URLs."""
|
|
386
|
+
params = {"urls": urls} if urls else {}
|
|
387
|
+
res = await self.send("Network.getCookies", params)
|
|
388
|
+
return res.get("cookies", [])
|
|
389
|
+
|
|
390
|
+
async def set_cookies(self, cookies: List[Dict[str, Any]]):
|
|
391
|
+
"""Injects cookies into the active session."""
|
|
392
|
+
await self.send("Network.setCookies", {"cookies": cookies})
|
|
393
|
+
|
|
394
|
+
async def clear_cookies(self):
|
|
395
|
+
"""Clears all browser cookies."""
|
|
396
|
+
await self.send("Network.clearBrowserCookies")
|
|
397
|
+
|
|
398
|
+
async def save_cookies(self, file_path: str):
|
|
399
|
+
"""Exports session cookies to a JSON file."""
|
|
400
|
+
cookies = await self.get_cookies()
|
|
401
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
402
|
+
json.dump(cookies, f, indent=2)
|
|
403
|
+
|
|
404
|
+
async def load_cookies(self, file_path: str):
|
|
405
|
+
"""Loads and applies cookies from a JSON file."""
|
|
406
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
407
|
+
cookies = json.load(f)
|
|
408
|
+
await self.set_cookies(cookies)
|
|
409
|
+
|
|
410
|
+
# =========================================================================
|
|
411
|
+
# Utility & Teardown
|
|
412
|
+
# =========================================================================
|
|
413
|
+
|
|
414
|
+
async def screenshot(self, file_path: str):
|
|
415
|
+
"""Captures viewport screenshot and saves to file."""
|
|
416
|
+
data = await self.send("Page.captureScreenshot", {"format": "png"})
|
|
417
|
+
raw_bytes = base64.b64decode(data["data"])
|
|
418
|
+
with open(file_path, "wb") as f:
|
|
419
|
+
f.write(raw_bytes)
|
|
420
|
+
|
|
421
|
+
async def close(self):
|
|
422
|
+
"""Gracefully closes socket, terminates browser process, and cleans temp storage."""
|
|
423
|
+
if self._receive_task:
|
|
424
|
+
self._receive_task.cancel()
|
|
425
|
+
if self.ws:
|
|
426
|
+
await self.ws.close()
|
|
427
|
+
if self.process:
|
|
428
|
+
self.process.terminate()
|
|
429
|
+
self.process.wait()
|
|
430
|
+
|
|
431
|
+
if self._is_temp_profile and os.path.exists(self.user_data_dir):
|
|
432
|
+
try:
|
|
433
|
+
shutil.rmtree(self.user_data_dir, ignore_errors=True)
|
|
434
|
+
except Exception:
|
|
435
|
+
pass
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import random
|
|
3
|
+
from typing import Any, Optional, Tuple
|
|
4
|
+
from .physics import generate_bezier_trajectory, human_keystroke_cadence
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Element:
|
|
8
|
+
"""
|
|
9
|
+
High-level abstraction for interacting with a DOM element via CDP.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, driver: Any, node_id: int, selector: str):
|
|
13
|
+
self.driver = driver
|
|
14
|
+
self.node_id = node_id
|
|
15
|
+
self.selector = selector
|
|
16
|
+
|
|
17
|
+
async def get_center_coordinates(self) -> Tuple[float, float]:
|
|
18
|
+
"""Calculates the center (x, y) coordinates of the element's content box."""
|
|
19
|
+
box = await self.driver.send("DOM.getBoxModel", {"nodeId": self.node_id})
|
|
20
|
+
quad = box["model"]["content"]
|
|
21
|
+
center_x = (quad[0] + quad[2] + quad[4] + quad[6]) / 4
|
|
22
|
+
center_y = (quad[1] + quad[3] + quad[5] + quad[7]) / 4
|
|
23
|
+
return center_x, center_y
|
|
24
|
+
|
|
25
|
+
async def scroll_into_view(self):
|
|
26
|
+
"""Scrolls the element into the visible viewport if necessary."""
|
|
27
|
+
try:
|
|
28
|
+
await self.driver.send("DOM.scrollIntoViewIfNeeded", {"nodeId": self.node_id})
|
|
29
|
+
await asyncio.sleep(0.1)
|
|
30
|
+
except Exception:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
async def is_visible(self) -> bool:
|
|
34
|
+
"""Checks if the element exists in the DOM and is physically visible."""
|
|
35
|
+
try:
|
|
36
|
+
box = await self.driver.send("DOM.getBoxModel", {"nodeId": self.node_id})
|
|
37
|
+
width = box["model"]["width"]
|
|
38
|
+
height = box["model"]["height"]
|
|
39
|
+
return width > 0 and height > 0
|
|
40
|
+
except Exception:
|
|
41
|
+
return False
|
|
42
|
+
|
|
43
|
+
async def click(self, human_curve: bool = True):
|
|
44
|
+
"""Moves mouse to element (using human-like Bezier curve trajectory) and clicks."""
|
|
45
|
+
await self.scroll_into_view()
|
|
46
|
+
target_x, target_y = await self.get_center_coordinates()
|
|
47
|
+
|
|
48
|
+
# Add sub-pixel human jitter
|
|
49
|
+
target_x += random.uniform(-2, 2)
|
|
50
|
+
target_y += random.uniform(-2, 2)
|
|
51
|
+
|
|
52
|
+
start_x = self.driver.current_mouse_x
|
|
53
|
+
start_y = self.driver.current_mouse_y
|
|
54
|
+
|
|
55
|
+
if human_curve:
|
|
56
|
+
trajectory = generate_bezier_trajectory(start_x, start_y, target_x, target_y)
|
|
57
|
+
for x, y, delay in trajectory:
|
|
58
|
+
await self.driver.send("Input.dispatchMouseEvent", {
|
|
59
|
+
"type": "mouseMoved",
|
|
60
|
+
"x": x,
|
|
61
|
+
"y": y
|
|
62
|
+
})
|
|
63
|
+
self.driver.current_mouse_x = x
|
|
64
|
+
self.driver.current_mouse_y = y
|
|
65
|
+
await asyncio.sleep(delay)
|
|
66
|
+
else:
|
|
67
|
+
await self.driver.send("Input.dispatchMouseEvent", {
|
|
68
|
+
"type": "mouseMoved",
|
|
69
|
+
"x": target_x,
|
|
70
|
+
"y": target_y
|
|
71
|
+
})
|
|
72
|
+
self.driver.current_mouse_x = target_x
|
|
73
|
+
self.driver.current_mouse_y = target_y
|
|
74
|
+
|
|
75
|
+
# Natural mousedown -> pause -> mouseup
|
|
76
|
+
await asyncio.sleep(random.uniform(0.04, 0.09))
|
|
77
|
+
await self.driver.send("Input.dispatchMouseEvent", {
|
|
78
|
+
"type": "mousePressed",
|
|
79
|
+
"x": target_x,
|
|
80
|
+
"y": target_y,
|
|
81
|
+
"button": "left",
|
|
82
|
+
"clickCount": 1
|
|
83
|
+
})
|
|
84
|
+
await asyncio.sleep(random.uniform(0.06, 0.12))
|
|
85
|
+
await self.driver.send("Input.dispatchMouseEvent", {
|
|
86
|
+
"type": "mouseReleased",
|
|
87
|
+
"x": target_x,
|
|
88
|
+
"y": target_y,
|
|
89
|
+
"button": "left",
|
|
90
|
+
"clickCount": 1
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
async def clear(self):
|
|
94
|
+
"""Clears text from the element."""
|
|
95
|
+
await self.click()
|
|
96
|
+
# Select all (Ctrl+A) and hit Backspace
|
|
97
|
+
await self.driver.send("Input.dispatchKeyEvent", {
|
|
98
|
+
"type": "keyDown",
|
|
99
|
+
"modifiers": 2,
|
|
100
|
+
"windowsVirtualKeyCode": 65,
|
|
101
|
+
"key": "a"
|
|
102
|
+
})
|
|
103
|
+
await self.driver.send("Input.dispatchKeyEvent", {
|
|
104
|
+
"type": "keyUp",
|
|
105
|
+
"key": "a"
|
|
106
|
+
})
|
|
107
|
+
await asyncio.sleep(0.05)
|
|
108
|
+
await self.driver.send("Input.dispatchKeyEvent", {
|
|
109
|
+
"type": "keyDown",
|
|
110
|
+
"windowsVirtualKeyCode": 8,
|
|
111
|
+
"key": "Backspace"
|
|
112
|
+
})
|
|
113
|
+
await self.driver.send("Input.dispatchKeyEvent", {
|
|
114
|
+
"type": "keyUp",
|
|
115
|
+
"key": "Backspace"
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
async def type_text(self, text: str, natural_cadence: bool = True):
|
|
119
|
+
"""Types text character by character with natural timing intervals."""
|
|
120
|
+
if natural_cadence:
|
|
121
|
+
cadence = human_keystroke_cadence(text)
|
|
122
|
+
for char, delay in cadence:
|
|
123
|
+
await self.driver.send("Input.dispatchKeyEvent", {"type": "keyDown", "text": char})
|
|
124
|
+
await self.driver.send("Input.dispatchKeyEvent", {"type": "keyUp"})
|
|
125
|
+
await asyncio.sleep(delay)
|
|
126
|
+
else:
|
|
127
|
+
for char in text:
|
|
128
|
+
await self.driver.send("Input.dispatchKeyEvent", {"type": "keyDown", "text": char})
|
|
129
|
+
await self.driver.send("Input.dispatchKeyEvent", {"type": "keyUp"})
|
|
130
|
+
await asyncio.sleep(0.02)
|
|
131
|
+
|
|
132
|
+
async def fill(self, text: str):
|
|
133
|
+
"""Focuses element, clears existing contents, and types text."""
|
|
134
|
+
await self.clear()
|
|
135
|
+
await self.type_text(text)
|
|
136
|
+
|
|
137
|
+
async def text(self) -> str:
|
|
138
|
+
"""Returns the innerText of the element."""
|
|
139
|
+
js = f"document.querySelector({repr(self.selector)}) ? document.querySelector({repr(self.selector)}).innerText : ''"
|
|
140
|
+
return await self.driver.evaluate(js) or ""
|
|
141
|
+
|
|
142
|
+
async def attribute(self, name: str) -> Optional[str]:
|
|
143
|
+
"""Returns the specified HTML attribute value."""
|
|
144
|
+
js = f"document.querySelector({repr(self.selector)}) ? document.querySelector({repr(self.selector)}).getAttribute({repr(name)}) : null"
|
|
145
|
+
return await self.driver.evaluate(js)
|
|
146
|
+
|
|
147
|
+
async def value(self) -> Optional[str]:
|
|
148
|
+
"""Returns the current input value of the element."""
|
|
149
|
+
js = f"document.querySelector({repr(self.selector)}) ? document.querySelector({repr(self.selector)}).value : null"
|
|
150
|
+
return await self.driver.evaluate(js)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import random
|
|
3
|
+
from typing import List, Tuple
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def generate_bezier_trajectory(
|
|
7
|
+
start_x: float,
|
|
8
|
+
start_y: float,
|
|
9
|
+
end_x: float,
|
|
10
|
+
end_y: float,
|
|
11
|
+
steps: int = 25,
|
|
12
|
+
) -> List[Tuple[float, float, float]]:
|
|
13
|
+
"""
|
|
14
|
+
Generates a human-like cubic Bezier trajectory from start to end coordinates.
|
|
15
|
+
Returns a list of (x, y, delay_seconds) tuples with natural acceleration/deceleration.
|
|
16
|
+
"""
|
|
17
|
+
distance = math.hypot(end_x - start_x, end_y - start_y)
|
|
18
|
+
if distance < 5:
|
|
19
|
+
return [(end_x, end_y, 0.01)]
|
|
20
|
+
|
|
21
|
+
# Dynamically scale steps based on distance
|
|
22
|
+
adjusted_steps = max(15, min(steps, int(distance / 12)))
|
|
23
|
+
|
|
24
|
+
# Generate two randomized control points with arc deviation
|
|
25
|
+
deviation = min(distance * 0.35, 120)
|
|
26
|
+
angle = math.atan2(end_y - start_y, end_x - start_x)
|
|
27
|
+
normal_angle = angle + math.pi / 2
|
|
28
|
+
|
|
29
|
+
# Control point 1 (closer to start)
|
|
30
|
+
cp1_dist = distance * random.uniform(0.2, 0.4)
|
|
31
|
+
cp1_dev = random.uniform(-deviation, deviation)
|
|
32
|
+
cx1 = start_x + math.cos(angle) * cp1_dist + math.cos(normal_angle) * cp1_dev
|
|
33
|
+
cy1 = start_y + math.sin(angle) * cp1_dist + math.sin(normal_angle) * cp1_dev
|
|
34
|
+
|
|
35
|
+
# Control point 2 (closer to target)
|
|
36
|
+
cp2_dist = distance * random.uniform(0.6, 0.8)
|
|
37
|
+
cp2_dev = random.uniform(-deviation * 0.7, deviation * 0.7)
|
|
38
|
+
cx2 = start_x + math.cos(angle) * cp2_dist + math.cos(normal_angle) * cp2_dev
|
|
39
|
+
cy2 = start_y + math.sin(angle) * cp2_dist + math.sin(normal_angle) * cp2_dev
|
|
40
|
+
|
|
41
|
+
trajectory = []
|
|
42
|
+
for i in range(1, adjusted_steps + 1):
|
|
43
|
+
t_linear = i / adjusted_steps
|
|
44
|
+
t = (math.sin((t_linear - 0.5) * math.pi) + 1.0) / 2.0
|
|
45
|
+
|
|
46
|
+
# Cubic Bezier formula: (1-t)^3*P0 + 3(1-t)^2*t*P1 + 3(1-t)*t^2*P2 + t^3*P3
|
|
47
|
+
u = 1.0 - t
|
|
48
|
+
px = (u**3 * start_x) + (3 * u**2 * t * cx1) + (3 * u * t**2 * cx2) + (t**3 * end_x)
|
|
49
|
+
py = (u**3 * start_y) + (3 * u**2 * t * cy1) + (3 * u * t**2 * cy2) + (t**3 * end_y)
|
|
50
|
+
|
|
51
|
+
# Micro-tremor / hand jitter (0.5 to 1.5 pixels)
|
|
52
|
+
jitter_x = random.uniform(-0.6, 0.6)
|
|
53
|
+
jitter_y = random.uniform(-0.6, 0.6)
|
|
54
|
+
|
|
55
|
+
# Micro delays: slower near start & end, faster in mid-flight
|
|
56
|
+
step_delay = random.uniform(0.005, 0.015)
|
|
57
|
+
if i < 4 or i > adjusted_steps - 4:
|
|
58
|
+
step_delay += random.uniform(0.01, 0.02)
|
|
59
|
+
|
|
60
|
+
trajectory.append((px + jitter_x, py + jitter_y, step_delay))
|
|
61
|
+
|
|
62
|
+
trajectory[-1] = (end_x, end_y, random.uniform(0.02, 0.05))
|
|
63
|
+
return trajectory
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def human_keystroke_cadence(
|
|
67
|
+
text: str,
|
|
68
|
+
base_delay: float = 0.04,
|
|
69
|
+
variation: float = 0.06,
|
|
70
|
+
) -> List[Tuple[str, float]]:
|
|
71
|
+
"""
|
|
72
|
+
Models realistic human typing cadence with inter-key variability,
|
|
73
|
+
longer pauses after punctuation/spaces, and natural typing rhythm.
|
|
74
|
+
"""
|
|
75
|
+
plan = []
|
|
76
|
+
for char in text:
|
|
77
|
+
delay = max(0.02, random.normalvariate(base_delay, variation))
|
|
78
|
+
if char == " ":
|
|
79
|
+
delay += random.uniform(0.05, 0.12)
|
|
80
|
+
elif char in [".", ",", "!", "?", "\n"]:
|
|
81
|
+
delay += random.uniform(0.15, 0.30)
|
|
82
|
+
plan.append((char, delay))
|
|
83
|
+
return plan
|
|
File without changes
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Stealth and anti-fingerprinting patches injected before DOM evaluation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
HARDENED_STEALTH_SCRIPT = """
|
|
6
|
+
(() => {
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
// 1. Mask navigator.webdriver
|
|
10
|
+
Object.defineProperty(Object.getPrototypeOf(navigator), 'webdriver', {
|
|
11
|
+
get: () => undefined,
|
|
12
|
+
configurable: true,
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// 2. Mock Permissions API (Notification permission standard prompt)
|
|
17
|
+
if (window.navigator.permissions) {
|
|
18
|
+
const originalQuery = window.navigator.permissions.query;
|
|
19
|
+
window.navigator.permissions.query = (parameters) => {
|
|
20
|
+
if (parameters && parameters.name === 'notifications') {
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
state: 'prompt',
|
|
23
|
+
onchange: null,
|
|
24
|
+
addEventListener: () => {},
|
|
25
|
+
removeEventListener: () => {},
|
|
26
|
+
dispatchEvent: () => false
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return originalQuery.call(window.navigator.permissions, parameters);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 3. Mock window.chrome runtime & desktop interfaces
|
|
34
|
+
if (!window.chrome) {
|
|
35
|
+
window.chrome = {};
|
|
36
|
+
}
|
|
37
|
+
window.chrome.app = window.chrome.app || {
|
|
38
|
+
isInstalled: false,
|
|
39
|
+
InstallState: { DISABLED: 'disabled', INSTALLED: 'installed', NOT_INSTALLED: 'not_installed' },
|
|
40
|
+
RunningState: { CANNOT_RUN: 'cannot_run', READY_TO_RUN: 'ready_to_run', RUNNING: 'running' }
|
|
41
|
+
};
|
|
42
|
+
window.chrome.csi = window.chrome.csi || function() {
|
|
43
|
+
return { startE: Date.now(), onloadT: Date.now() + 500, pageT: 520, tran: 15 };
|
|
44
|
+
};
|
|
45
|
+
window.chrome.loadTimes = window.chrome.loadTimes || function() {
|
|
46
|
+
return {
|
|
47
|
+
commitLoadTime: Date.now() / 1000,
|
|
48
|
+
connectionInfo: 'h2',
|
|
49
|
+
finishDocumentLoadTime: Date.now() / 1000 + 0.3,
|
|
50
|
+
finishLoadTime: Date.now() / 1000 + 0.5,
|
|
51
|
+
firstPaintAfterLoadTime: 0,
|
|
52
|
+
firstPaintTime: Date.now() / 1000 + 0.1,
|
|
53
|
+
navigationType: 'Other',
|
|
54
|
+
npnNegotiatedProtocol: 'h2',
|
|
55
|
+
requestTime: Date.now() / 1000 - 0.2,
|
|
56
|
+
startLoadTime: Date.now() / 1000
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
if (!window.chrome.runtime) {
|
|
60
|
+
window.chrome.runtime = {
|
|
61
|
+
OnInstalledReason: { CHROME_UPDATE: 'chrome_update', INSTALL: 'install', SHARED_MODULE_UPDATE: 'shared_module_update', UPDATE: 'update' },
|
|
62
|
+
PlatformArch: { ARM: 'arm', ARM64: 'arm64', MIPS: 'mips', MIPS64: 'mips64', X86_32: 'x86-32', X86_64: 'x86-64' },
|
|
63
|
+
PlatformNaclArch: { ARM: 'arm', MIPS: 'mips', MIPS64: 'mips64', X86_32: 'x86-32', X86_64: 'x86-64' },
|
|
64
|
+
PlatformOs: { ANDROID: 'android', CROS: 'cros', LINUX: 'linux', MAC: 'mac', OPENBSD: 'openbsd', WIN: 'win' },
|
|
65
|
+
RequestUpdateCheckStatus: { NO_UPDATE: 'no_update', THROTTLED: 'throttled', UPDATE_AVAILABLE: 'update_available' }
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 4. Mock navigator.plugins & navigator.mimeTypes
|
|
70
|
+
const fakePluginsData = [
|
|
71
|
+
{
|
|
72
|
+
name: 'PDF Viewer',
|
|
73
|
+
filename: 'internal-pdf-viewer',
|
|
74
|
+
description: 'Portable Document Format',
|
|
75
|
+
mimeTypes: [{ type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format' }]
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'Chrome PDF Viewer',
|
|
79
|
+
filename: 'internal-pdf-viewer',
|
|
80
|
+
description: 'Portable Document Format',
|
|
81
|
+
mimeTypes: [{ type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format' }]
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'Chromium PDF Viewer',
|
|
85
|
+
filename: 'internal-pdf-viewer',
|
|
86
|
+
description: 'Portable Document Format',
|
|
87
|
+
mimeTypes: [{ type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format' }]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'Microsoft Edge PDF Viewer',
|
|
91
|
+
filename: 'internal-pdf-viewer',
|
|
92
|
+
description: 'Portable Document Format',
|
|
93
|
+
mimeTypes: [{ type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format' }]
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: 'WebKit built-in PDF',
|
|
97
|
+
filename: 'internal-pdf-viewer',
|
|
98
|
+
description: 'Portable Document Format',
|
|
99
|
+
mimeTypes: [{ type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format' }]
|
|
100
|
+
}
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
if (navigator.plugins.length === 0) {
|
|
104
|
+
const pluginArray = Object.create(PluginArray.prototype);
|
|
105
|
+
fakePluginsData.forEach((p, idx) => {
|
|
106
|
+
const plugin = Object.create(Plugin.prototype);
|
|
107
|
+
Object.defineProperty(plugin, 'name', { get: () => p.name });
|
|
108
|
+
Object.defineProperty(plugin, 'filename', { get: () => p.filename });
|
|
109
|
+
Object.defineProperty(plugin, 'description', { get: () => p.description });
|
|
110
|
+
Object.defineProperty(plugin, 'length', { get: () => p.mimeTypes.length });
|
|
111
|
+
pluginArray[idx] = plugin;
|
|
112
|
+
pluginArray[p.name] = plugin;
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(pluginArray, 'length', { get: () => fakePluginsData.length });
|
|
115
|
+
Object.defineProperty(navigator, 'plugins', { get: () => pluginArray });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 5. Ensure navigator.languages consistency
|
|
119
|
+
if (!navigator.languages || navigator.languages.length === 0) {
|
|
120
|
+
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 6. WebGL Vendor & Renderer spoofing
|
|
124
|
+
const patchWebGL = (proto) => {
|
|
125
|
+
if (!proto) return;
|
|
126
|
+
const originalGetParameter = proto.getParameter;
|
|
127
|
+
proto.getParameter = function(param) {
|
|
128
|
+
if (param === 37445) return 'Google Inc. (NVIDIA)';
|
|
129
|
+
if (param === 37446) return 'ANGLE (NVIDIA, NVIDIA GeForce RTX 3070 Direct3D11 vs_5_0 ps_5_0, D3D11)';
|
|
130
|
+
return originalGetParameter.apply(this, arguments);
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
patchWebGL(window.WebGLRenderingContext && window.WebGLRenderingContext.prototype);
|
|
134
|
+
patchWebGL(window.WebGL2RenderingContext && window.WebGL2RenderingContext.prototype);
|
|
135
|
+
|
|
136
|
+
// 7. Screen & Window Geometry Alignment
|
|
137
|
+
if (window.outerWidth === 0 && window.innerWidth > 0) {
|
|
138
|
+
Object.defineProperty(window, 'outerWidth', { get: () => window.innerWidth });
|
|
139
|
+
Object.defineProperty(window, 'outerHeight', { get: () => window.innerHeight + 85 });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 8. Canvas Hash Randomization (imperceptible noise)
|
|
143
|
+
const originalToDataURL = HTMLCanvasElement.prototype.toDataURL;
|
|
144
|
+
HTMLCanvasElement.prototype.toDataURL = function(type) {
|
|
145
|
+
const ctx = this.getContext('2d');
|
|
146
|
+
if (ctx && this.width > 16 && this.height > 16) {
|
|
147
|
+
try {
|
|
148
|
+
const imgData = ctx.getImageData(0, 0, 1, 1);
|
|
149
|
+
imgData.data[0] = (imgData.data[0] ^ 1);
|
|
150
|
+
ctx.putImageData(imgData, 0, 0);
|
|
151
|
+
} catch(e) {}
|
|
152
|
+
}
|
|
153
|
+
return originalToDataURL.apply(this, arguments);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
})();
|
|
157
|
+
"""
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: strawdriver
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stealth, high-performance Chromium automation driver over Chrome DevTools Protocol (CDP).
|
|
5
|
+
Author: StrawDriver Contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/strawdriver/strawdriver
|
|
8
|
+
Keywords: automation,chromium,cdp,chrome-devtools-protocol,stealth,webdriver
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: websockets>=12.0
|
|
23
|
+
|
|
24
|
+
# StrawDriver
|
|
25
|
+
|
|
26
|
+
A standalone, stealth Chromium automation driver communicating directly via the Chrome DevTools Protocol (CDP) over WebSockets.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Architecture
|
|
31
|
+
|
|
32
|
+
- **`strawdriver/driver.py`**: Core `StrawDriver` client managing browser lifecycle, tabs, cookies, and CDP transport.
|
|
33
|
+
- **`strawdriver/element.py`**: `Element` abstraction for querying, scrolling, clicking, and filling fields.
|
|
34
|
+
- **`strawdriver/physics.py`**: Bézier curve trajectory generator and natural typing cadence.
|
|
35
|
+
- **`strawdriver/stealth.py`**: Pre-document injections to mask automation indicators and normalize browser properties.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install locally in editable mode:
|
|
42
|
+
|
|
43
|
+
```powershell
|
|
44
|
+
cd "C:\Users\Admin\Desktop\VaultO'AllMighty\webdriver"
|
|
45
|
+
pip install -e .
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Quickstart
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import asyncio
|
|
54
|
+
from strawdriver import StrawDriver
|
|
55
|
+
|
|
56
|
+
async def main():
|
|
57
|
+
driver = StrawDriver(headless=False)
|
|
58
|
+
await driver.start()
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
await driver.goto("https://example.com", wait_until_network_idle=True)
|
|
62
|
+
element = await driver.wait_for_selector("a")
|
|
63
|
+
print("Link text:", await element.text())
|
|
64
|
+
finally:
|
|
65
|
+
await driver.close()
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
asyncio.run(main())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Running Tests
|
|
74
|
+
|
|
75
|
+
Execute the unit test suite:
|
|
76
|
+
|
|
77
|
+
```powershell
|
|
78
|
+
python -m unittest discover -s tests
|
|
79
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
strawdriver/__init__.py
|
|
4
|
+
strawdriver/driver.py
|
|
5
|
+
strawdriver/element.py
|
|
6
|
+
strawdriver/physics.py
|
|
7
|
+
strawdriver/py.typed
|
|
8
|
+
strawdriver/stealth.py
|
|
9
|
+
strawdriver.egg-info/PKG-INFO
|
|
10
|
+
strawdriver.egg-info/SOURCES.txt
|
|
11
|
+
strawdriver.egg-info/dependency_links.txt
|
|
12
|
+
strawdriver.egg-info/requires.txt
|
|
13
|
+
strawdriver.egg-info/top_level.txt
|
|
14
|
+
tests/test_driver.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
websockets>=12.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
strawdriver
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
from strawdriver import StrawDriver
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestStrawDriver(unittest.IsolatedAsyncioTestCase):
|
|
8
|
+
async def asyncSetUp(self):
|
|
9
|
+
self.driver = StrawDriver(headless=True, port=9399)
|
|
10
|
+
await self.driver.start()
|
|
11
|
+
|
|
12
|
+
async def asyncTearDown(self):
|
|
13
|
+
await self.driver.close()
|
|
14
|
+
|
|
15
|
+
async def test_stealth_flags(self):
|
|
16
|
+
await self.driver.goto("https://example.com")
|
|
17
|
+
webdriver_flag = await self.driver.evaluate("navigator.webdriver")
|
|
18
|
+
self.assertIn(webdriver_flag, [None, False])
|
|
19
|
+
chrome_runtime = await self.driver.evaluate("Boolean(window.chrome && window.chrome.runtime)")
|
|
20
|
+
self.assertTrue(chrome_runtime)
|
|
21
|
+
|
|
22
|
+
async def test_element_api(self):
|
|
23
|
+
await self.driver.goto("https://example.com")
|
|
24
|
+
p_element = await self.driver.wait_for_selector("p", timeout=10.0)
|
|
25
|
+
self.assertIsNotNone(p_element)
|
|
26
|
+
self.assertTrue(await p_element.is_visible())
|
|
27
|
+
text = await p_element.text()
|
|
28
|
+
self.assertTrue(len(text) > 0)
|
|
29
|
+
|
|
30
|
+
async def test_tab_management(self):
|
|
31
|
+
initial_tabs = await self.driver.list_tabs()
|
|
32
|
+
tab2 = await self.driver.new_tab("about:blank")
|
|
33
|
+
tabs_after_open = await self.driver.list_tabs()
|
|
34
|
+
self.assertEqual(len(tabs_after_open), len(initial_tabs) + 1)
|
|
35
|
+
await self.driver.close_tab(tab2)
|
|
36
|
+
tabs_after_close = await self.driver.list_tabs()
|
|
37
|
+
self.assertEqual(len(tabs_after_close), len(initial_tabs))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
if __name__ == "__main__":
|
|
41
|
+
unittest.main()
|