tot_agent.browser¶
Playwright browser context pool.
Class diagram¶
classDiagram
class BrowserManager {
+headless: bool
+site_url: str
-_pw: Playwright
-_browser: Browser
-_contexts: dict
-_active_user: str
+__aenter__() BrowserManager
+__aexit__(*_) None
+switch_user(user_key) dict
+active_page() Page
+active_user() str
+navigate(url) dict
+screenshot() str
+click(selector) dict
+fill(selector, value) dict
+select_option(selector, value) dict
+get_page_text() dict
+get_page_url() dict
+wait_for_selector(selector, timeout) dict
+wait_for_page_ready() dict
+press_key(key) dict
+scroll_down() dict
+evaluate(js) dict
+upload_file(selector, file_path) dict
}
Context lifecycle¶
stateDiagram-v2
[*] --> Stopped
Stopped --> Running : __aenter__()
Running --> Active : switch_user()
Active --> Active : navigate() / click() / fill() / …
Active --> Running : switch_user() (different user)
Running --> Stopped : __aexit__()
Structured results¶
All action methods return a dict with a consistent shape produced by helpers
in results.py:
| Key | Type | Description |
|---|---|---|
ok |
bool |
True on success, False on failure |
message |
str |
Human-readable summary |
data |
dict |
Method-specific payload (e.g. {"url": "..."}) |
error |
str |
Error detail (only present when ok is False) |
recoverable |
bool |
True when a timeout or transient error occurred |
Module reference¶
tot_agent.browser
¶
browser.py — Playwright browser manager.
Maintains a pool of named browser contexts so the agent can switch between simulated users without re-launching the browser. Each context has isolated cookies / session storage, simulating a distinct logged-in user.
Usage::
async with BrowserManager(headless=True) as bm:
await bm.switch_user("admin")
await bm.navigate("/dashboard")
b64_png = await bm.screenshot()
BrowserManager
¶
Owns the Playwright instance and a pool of named browser contexts.
Use as an async context manager::
async with BrowserManager(headless=True) as bm:
await bm.switch_user("alice")
await bm.navigate("/login")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headless
|
bool
|
When |
False
|
site_url
|
str
|
Base URL prepended to relative navigation paths.
Defaults to :data: |
SITE_URL
|
Source code in src/tot_agent/browser.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
active_page
property
¶
The :class:~playwright.async_api.Page for the active user.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no user context has been activated yet. |
active_user
property
¶
Username of the currently active browser context, or None.
__aenter__()
async
¶
Start Playwright and launch the Chromium browser.
Returns:
| Type | Description |
|---|---|
BrowserManager
|
This :class: |
Source code in src/tot_agent/browser.py
__aexit__(*_)
async
¶
Close all browser contexts and stop Playwright.
Source code in src/tot_agent/browser.py
switch_user(user_key)
async
¶
Make user_key the active browser context, creating it if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_key
|
str
|
Username to switch to. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A status string confirming the switch. |
Source code in src/tot_agent/browser.py
navigate(url)
async
¶
Navigate the active page to url.
If url starts with / it is treated as a relative path and
prepended with :attr:site_url.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Absolute URL or site-relative path (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string with the resolved URL. |
Source code in src/tot_agent/browser.py
screenshot()
async
¶
Capture a screenshot of the active page.
Returns:
| Type | Description |
|---|---|
str
|
Base64-encoded PNG image data. |
Source code in src/tot_agent/browser.py
click(selector)
async
¶
Click the first element matching selector.
Tries CSS selector first; falls back to visible-text matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector or visible text to click. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string, or an error message prefixed with
|
Source code in src/tot_agent/browser.py
fill(selector, value)
async
¶
Clear and fill an input field identified by selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector of the input element. |
required |
value
|
str
|
Text to type into the field. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string, or an error message prefixed with
|
Source code in src/tot_agent/browser.py
select_option(selector, value)
async
¶
Select a <select> option by value or label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector of the |
required |
value
|
str
|
Option value or visible label to select. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string, or an error message prefixed with
|
Source code in src/tot_agent/browser.py
get_page_text()
async
¶
Return visible body text of the active page, capped at 4 000 chars.
Returns:
| Type | Description |
|---|---|
str
|
Truncated visible text content. |
Source code in src/tot_agent/browser.py
get_page_url()
async
¶
Return the current URL of the active page.
Returns:
| Type | Description |
|---|---|
str
|
Current page URL. |
Source code in src/tot_agent/browser.py
wait_for_selector(selector, timeout=WAIT_FOR_ELEMENT_TIMEOUT_MS)
async
¶
Wait until a CSS selector appears on the page.
Useful for waiting after form submissions or client-side route changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector to wait for. |
required |
timeout
|
int
|
Maximum wait time in milliseconds. Defaults to
|
WAIT_FOR_ELEMENT_TIMEOUT_MS
|
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string, or a timeout error message. |
Source code in src/tot_agent/browser.py
press_key(key)
async
¶
Press a keyboard key on the active page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Playwright key name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string. |
Source code in src/tot_agent/browser.py
scroll_down()
async
¶
Scroll the active page to the bottom.
Returns:
| Type | Description |
|---|---|
str
|
Confirmation string. |
Source code in src/tot_agent/browser.py
evaluate(js)
async
¶
Execute arbitrary JavaScript in the active page context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
js
|
str
|
JavaScript expression or statement to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation of the result (capped at 1 000 chars), or a JS error message. |
Source code in src/tot_agent/browser.py
wait_for_page_ready(timeout=PAGE_READY_TIMEOUT_MS)
async
¶
Best-effort wait for a DOM-ready state after an action.
This is intentionally recoverable: many SPA interactions do not trigger a new document load, so timing out here is useful signal but not always a hard failure.
Source code in src/tot_agent/browser.py
upload_file(selector, file_path)
async
¶
Set a local file on a <input type="file"> element via Playwright.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector of the file input element. |
required |
file_path
|
str
|
Absolute path to the local file to upload. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Structured result dict. |