trading_agent.tools.browser_session
¶
One desk's Chrome browser, driven by Playwright.
Chrome runs headless with a profile of its own inside the desk's folder, so it never sees the user's logins, cookies or history. It identifies itself as ordinary Chrome rather than HeadlessChrome and hides the automation flag, because Indian finance sites such as Moneycontrol and Zee Business refuse headless browsers with HTTP 403.
Every request the browser makes, including images, scripts and redirects, passes the address guard, so no page can make it fetch anything from this machine or the local network, and file:// pages cannot be opened. Downloads are refused.
A snapshot gives every visible clickable element a number, so a model that cannot see pictures can still say "click 3" or "type into 7".
Typical usage example:
session = BrowserSession(profile_directory, screenshot_directory, guard, browser_configuration) snapshot = await session.open('https://www.nseindia.com/') await session.close()
BrowserError
¶
Bases: Exception
The browser could not do what was asked, for a reason the model can act on.
BrowserSession
¶
A desk's browser, started on first use.
Creates the session without starting Chrome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_directory
|
Path
|
The desk's own Chrome profile folder. |
required |
screenshot_directory
|
Path
|
Where screenshots are saved. |
required |
guard
|
AddressGuard
|
The check every request must pass. |
required |
browser_configuration
|
BrowserConfiguration
|
Timeouts and limits. |
required |
Source code in src/trading_agent/tools/browser_session.py
open
async
¶
Opens a page in the current tab and returns its snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
The page's address. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The page's snapshot. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
The address is refused, or the page could not be opened. |
AddressRefusedError
|
The address is not on the public internet. |
Source code in src/trading_agent/tools/browser_session.py
snapshot
async
¶
Lists the current page's clickable elements and text.
Returns:
| Type | Description |
|---|---|
str
|
The snapshot. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
No page is open yet. |
Source code in src/trading_agent/tools/browser_session.py
click
async
¶
Clicks a numbered element from the last snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
int
|
The element's number. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The snapshot after the click. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
No element has that number, or the click failed. |
Source code in src/trading_agent/tools/browser_session.py
type_text
async
¶
Types into a numbered field, replacing what is in it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
int
|
The field's number from the last snapshot. |
required |
text
|
str
|
What to type. |
required |
submit
|
bool
|
Whether to press Enter afterwards. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The snapshot after typing. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
No field has that number, or typing failed. |
Source code in src/trading_agent/tools/browser_session.py
screenshot
async
¶
Takes a screenshot of the visible part of the current page.
Returns:
| Type | Description |
|---|---|
tuple[bytes, Path]
|
The PNG picture and where it was saved. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
No page is open yet. |
Source code in src/trading_agent/tools/browser_session.py
back
async
¶
Goes back one page in the current tab.
Returns:
| Type | Description |
|---|---|
str
|
The snapshot of the previous page. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
There is no earlier page. |
Source code in src/trading_agent/tools/browser_session.py
tabs
async
¶
Lists, switches to, or closes tabs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
"list", "switch" or "close". |
required |
index
|
int | None
|
The tab's number from the list, for "switch" and "close". |
required |
Returns:
| Type | Description |
|---|---|
str
|
The list of tabs after the action, with the current one marked. |
Raises:
| Type | Description |
|---|---|
BrowserError
|
The tab number does not exist, or no number was given. |
Source code in src/trading_agent/tools/browser_session.py
close
async
¶
Closes Chrome and stops Playwright.