Python Examples
Explain Python examples by quickstart flow, use case, command, and expected result.
Read the examples from basic to advanced. Start with demo.py to confirm that the SDK can create a session and connect to the remote browser. Then choose View debugging, downloads, context, extensions, proxy, or light mode based on your scenario.
Before running examples
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Fill LEXMOUNT_API_KEY and LEXMOUNT_PROJECT_IDCommon .env fields:
LEXMOUNT_API_KEY=sk_...
LEXMOUNT_PROJECT_ID=project_...
LEXMOUNT_BASE_URL=https://api.lexmount.cnRecommended order
| Order | Example | Purpose |
|---|---|---|
| 1 | demo.py | Minimal loop: create session, connect browser, visit page |
| 2 | inspect_url_demo.py | Keep remote View open while debugging |
| 3 | session_list.py | List current and historical sessions |
| 4 | session_downloads.py | Retrieve files downloaded by the remote browser |
| 5 | context_basic.py | Persist and reuse browser context |
| 6 | extension_basic.py | Upload and mount Chrome extension |
| 7 | proxy_demo.py | Create session with custom proxy |
| 8 | light_demo.py | Understand light browser mode for content extraction |
demo.py: minimal browser loop
Run:
python3 demo.pyWhat it does:
- Initializes
Lexmount()from environment variables. - Calls
client.sessions.create()to create one cloud browser. - Uses
session.connect_urlto connect through Playwright CDP. - Opens a page, reads the title, and saves a screenshot.
- Closes the browser connection and session.
Use this to confirm that the API Key is valid, quota is available, and Playwright can control the remote browser.
inspect_url_demo.py: keep remote View open
Run:
python3 inspect_url_demo.pyIt prints inspect_url and keeps the session alive until you press enter. This maps to the View step in the console quickstart and helps debug:
- whether the page opens in the remote browser;
- where the agent or script currently is;
- whether a human needs to copy, paste, or click.
session_list.py: inspect project sessions
Run:
python3 session_list.pyUse it to:
- check whether browsers were not released;
- inspect active and closed sessions;
- verify instance count during parallel task debugging.
session_downloads.py: retrieve downloaded files
Run:
python3 session_downloads.pyDownloaded files live in the remote browser, not on your local machine. This example shows the full path:
- Configure download behavior through CDP.
- Trigger a file download in the remote page.
- Query downloads through the SDK.
- Fetch one file or archive all downloads.
Use this for agent tasks that produce PDF, CSV, ZIP, or other downloaded artifacts.
context_basic.py: reuse browser context
Run:
python3 context_basic.pyContext stores browser data such as cookies, login state, and local storage. Use it when tasks need to reuse state across runs.
Note that a context can be locked by an active session. Parallel tasks should handle lock and release behavior.
extension_basic.py: upload and mount extension
Run:
python3 extension_basic.pyIt shows how to:
- upload an extension zip;
- list uploaded extensions;
- mount extensions with
extension_idswhen creating a session.
Only use this layer when the browser task depends on Chrome extension behavior.
proxy_demo.py: custom proxy
Run:
python3 proxy_demo.pyIt shows how to pass proxy when creating a session. Use it for upstream proxy, regional egress, or authenticated proxy scenarios.
light_demo.py: light browser mode
Run:
python3 light_demo.pyThis example explains light browser usage for content extraction scenarios. It is not the first demo to run; read it after demo.py and inspect_url_demo.py both work.
Troubleshooting
Authentication failed
Check that .env contains LEXMOUNT_API_KEY and LEXMOUNT_PROJECT_ID, and that the Project ID belongs to the same project as the API Key.
Playwright cannot connect
Print both session.connect_url and session.inspect_url. If View opens but CDP cannot connect, check network access, base URL, and whether the session was already closed.
Session was not released
Prefer context managers:
with client.sessions.create() as session:
...This gives the SDK a chance to release the session even if the script fails.
Lexmount Docs