Skip to main content

Managing Browser Sessions

In this guide, you'll learn how to manage browser sessions effectively using the Aidolon Browser Client. Proper session management is crucial for saving resources, maintaining continuity (like preserving login cookies), and controlling your usage costs. By the end of this tutorial, you'll understand how to create, list, get context from, and close browser sessions.


What You'll Learn

  • Creating and managing browser sessions
  • Preserving login sessions with context
  • Listing active and closed sessions
  • Safely closing browser sessions
  • Best practices for session management

Session Lifecycle Overview

Understanding the session lifecycle helps you automate browsers effectively and cost-efficiently. Here's how a typical session lifecycle looks:

Let's break it down step-by-step.


1. Creating a Browser Session

To automate a browser, first create a session. This starts the clock on session credits (sessions are charged based on time).

Here's how to create a session:

from aidolon_browser_client.browser.browser_session import BrowserSession

# Recommended context manager pattern
with BrowserSession() as browser:
browser.navigate("https://example.com")
# Browser automation tasks go here

Or manually (make sure to close it later!):

browser = BrowserSession()
browser.navigate("https://example.com")

Sessions default to a 5-minute timeout, but you can set your own timeout:

# Creating a session with a custom timeout (in seconds)
from aidolon_browser_client.browser.browser_session import create_session

browser = create_session(timeout=600) # 10 minutes

2. Listing Your Browser Sessions

Want to see which sessions are currently active or already closed? You can quickly get a list of your browser sessions:

from aidolon_browser_client.sessions.sessions import list_all_sessions

# List all sessions (active and closed)
all_sessions = list_all_sessions()

# Optionally, filter sessions by status
active_sessions = list_all_sessions(status="active")
closed_sessions = list_all_sessions(status="closed")

print("Active sessions:", active_sessions)
print("Closed sessions:", closed_sessions)

3. Saving and Reusing Session Context

Session contexts (cookies, local storage, etc.) allow you to preserve your login status between sessions. No more logging in every time!

Here's how to save a session's context for later use:

with BrowserSession() as browser:
browser.navigate("https://example.com")
browser.type_text("#username", "your-username", delay=0.1)
browser.type_text("#password", "your-password", delay=0.1)
browser.click("#login-button")

# Save session context for later
context = browser.get_context()

Reusing the context in a new session:

from aidolon_browser_client.browser.browser_session import create_session

# Recreate session using previous context
browser = create_session(context=context)

browser.navigate("https://example.com") # Already logged in!

This powerful feature saves you time, credits, and frustration.


4. Closing Browser Sessions

Always close sessions when you're finished! This releases resources immediately and helps manage your usage costs:

# Close session manually
browser.close_session()

If you have multiple sessions open or forgot to close some:

from aidolon_browser_client.sessions.sessions import close_all_sessions

close_all_sessions() # closes all active sessions instantly

When in doubt, closing all sessions is a safe bet.


Complete Session Lifecycle Example

Here's how all these steps come together:

from aidolon_browser_client.browser.browser_session import BrowserSession, create_session
from aidolon_browser_client.sessions.sessions import close_all_sessions, list_all_sessions

# Step 1: Create and use a browser session
with BrowserSession() as browser:
browser.navigate("https://example.com/login")
browser.type("#username", "user")
browser.type("#password", "pass")
browser.click("login button")

# Step 2: Save the context for reuse
saved_context = browser.get_context()

# Step 3: Verify session closed automatically
print("Sessions after automatic close:", list_all_sessions(status="active"))

# Step 4: Reuse saved context to skip login next time
browser = create_session(context=saved_context, timeout=600) # 10-minute session
browser.navigate("https://example.com/dashboard")

# Perform more tasks...

# Always close sessions when done
browser.close_session()

# If unsure, safely close all remaining sessions
close_all_sessions()

Best Practices for Session Management

Follow these best practices for efficient session management:

  • Use context managers (with blocks) whenever possible.
  • Explicitly close sessions to minimize usage costs.
  • Set appropriate session timeouts based on your use case.
  • Use close_all_sessions() when in doubt.
  • Always reuse session contexts to save credits and avoid repeated logins.

You're now ready to manage Aidolon browser sessions like a pro!