Skip to main content

Overview

This example demonstrates Tzafon’s multi-tab browser management and page context features by comparing Wikipedia articles across multiple tabs. You’ll learn how to:
  • Create and manage multiple browser tabs
  • Execute actions on specific tabs
  • Use page context to track state
  • Switch between tabs
  • Capture screenshots from each tab
This example uses browser-only features. Multi-tab management is not available for desktop instances.

Use Case

You want to research multiple topics simultaneously and capture information from each. This pattern applies to:
  • Comparison shopping - Compare prices across multiple e-commerce sites
  • Research - Gather information from multiple sources
  • Monitoring - Track changes across multiple dashboards
  • Testing - Verify consistency across multiple pages

Complete Example

from tzafon import Computer

# Topics to research
topics = ["Quantum Computing", "Artificial Intelligence", "Blockchain"]

client = Computer()
with client.create(kind="browser") as computer:
    # Navigate main tab to Wikipedia
    computer.navigate("https://wikipedia.org")
    computer.wait(2)

    # Get the main tab ID
    result = computer.execute_action({"type": "list_tabs"})
    tabs = result.result['tabs']
    main_tab_id = tabs[0]['id']

    print(f"Main tab ID: {main_tab_id}")

    # Dictionary to store tab IDs for each topic
    topic_tabs = {
        "Main": main_tab_id
    }

    # Create a new tab for each topic
    for topic in topics:
        # Create new tab
        result = computer.execute_action({
            "type": "new_tab",
            "url": f"https://en.wikipedia.org/wiki/{topic.replace(' ', '_')}"
        })

        tab_id = result.executed_tab_id
        topic_tabs[topic] = tab_id

        print(f"Created tab for '{topic}': {tab_id}")

        # Wait for page to load
        computer.wait(2)

    # Now visit each tab and get page context
    print("\n--- Gathering Page Information ---\n")

    for topic, tab_id in topic_tabs.items():
        if topic == "Main":
            continue

        # Switch to this tab
        computer.execute_action({
            "type": "switch_tab",
            "tab_id": tab_id
        })

        # Get page context
        result = computer.execute_action({
            "type": "screenshot",
            "tab_id": tab_id,
            "include_context": True
        })

        context = result.page_context
        screenshot_url = result.result['screenshot_url']

        print(f"Topic: {topic}")
        print(f"  URL: {context.url}")
        print(f"  Title: {context.title}")
        print(f"  Viewport: {context.viewport_width}x{context.viewport_height}")
        print(f"  Page Size: {context.page_width}x{context.page_height}")
        print(f"  Scroll: ({context.scroll_x}, {context.scroll_y})")
        print(f"  Screenshot: {screenshot_url}")

        # Calculate how much content is below the fold
        visible_height = context.viewport_height
        total_height = context.page_height
        below_fold = total_height - visible_height

        if below_fold > 0:
            print(f"  Content below fold: {below_fold}px")

            # Scroll to see more content
            computer.execute_action({
                "type": "scroll",
                "dx": 0,
                "dy": 500,
                "tab_id": tab_id
            })
            computer.wait(1)

            # Take another screenshot after scrolling
            result = computer.execute_action({
                "type": "screenshot",
                "tab_id": tab_id,
                "include_context": True
            })

            print(f"  Scrolled screenshot: {result.result['screenshot_url']}")
            print(f"  New scroll position: {result.page_context.scroll_y}px")

        print()

    # List all tabs to verify
    print("--- All Open Tabs ---\n")
    result = computer.execute_action({"type": "list_tabs"})
    all_tabs = result.result['tabs']

    for tab in all_tabs:
        active = "✓" if tab.get('is_active') else " "
        print(f"[{active}] {tab['id']}: {tab['title']}")

    # Close all topic tabs, keep only main
    print("\n--- Cleaning Up ---\n")

    for topic, tab_id in topic_tabs.items():
        if topic != "Main":
            computer.execute_action({
                "type": "close_tab",
                "tab_id": tab_id
            })
            print(f"Closed tab: {topic}")

    # Verify only main tab remains
    result = computer.execute_action({"type": "list_tabs"})
    remaining = len(result.result['tabs'])
    print(f"\nTabs remaining: {remaining}")

Example Output

Main tab ID: tab_abc123

Created tab for 'Quantum Computing': tab_def456
Created tab for 'Artificial Intelligence': tab_ghi789
Created tab for 'Blockchain': tab_jkl012

--- Gathering Page Information ---

Topic: Quantum Computing
  URL: https://en.wikipedia.org/wiki/Quantum_Computing
  Title: Quantum Computing - Wikipedia
  Viewport: 1280x720
  Page Size: 1280x4500
  Scroll: (0, 0)
  Screenshot: https://storage.tzafon.ai/screenshots/qc1.png
  Content below fold: 3780px
  Scrolled screenshot: https://storage.tzafon.ai/screenshots/qc2.png
  New scroll position: 500px

Topic: Artificial Intelligence
  URL: https://en.wikipedia.org/wiki/Artificial_Intelligence
  Title: Artificial Intelligence - Wikipedia
  Viewport: 1280x720
  Page Size: 1280x6200
  Scroll: (0, 0)
  Screenshot: https://storage.tzafon.ai/screenshots/ai1.png
  Content below fold: 5480px
  Scrolled screenshot: https://storage.tzafon.ai/screenshots/ai2.png
  New scroll position: 500px

Topic: Blockchain
  URL: https://en.wikipedia.org/wiki/Blockchain
  Title: Blockchain - Wikipedia
  Viewport: 1280x720
  Page Size: 1280x5100
  Scroll: (0, 0)
  Screenshot: https://storage.tzafon.ai/screenshots/bc1.png
  Content below fold: 4380px
  Scrolled screenshot: https://storage.tzafon.ai/screenshots/bc2.png
  New scroll position: 500px

--- All Open Tabs ---

[✓] tab_abc123: Wikipedia
[ ] tab_def456: Quantum Computing - Wikipedia
[ ] tab_ghi789: Artificial Intelligence - Wikipedia
[ ] tab_jkl012: Blockchain - Wikipedia

--- Cleaning Up ---

Closed tab: Quantum Computing
Closed tab: Artificial Intelligence
Closed tab: Blockchain

Tabs remaining: 1

Key Techniques

1. Tab Management

# Create new tab
result = computer.execute_action({
    "type": "new_tab",
    "url": "https://example.com"
})
tab_id = result.executed_tab_id

# List all tabs
result = computer.execute_action({"type": "list_tabs"})
tabs = result.result['tabs']

# Switch to specific tab
computer.execute_action({
    "type": "switch_tab",
    "tab_id": tab_id
})

# Close tab
computer.execute_action({
    "type": "close_tab",
    "tab_id": tab_id
})

2. Page Context for Smart Scrolling

# Get page context to know dimensions
result = computer.execute_action({
    "type": "screenshot",
    "include_context": True
})

context = result.page_context

# Calculate content below viewport
visible = context.viewport_height
total = context.page_height
below_fold = total - visible

# Scroll intelligently
if below_fold > 0:
    computer.scroll(dx=0, dy=500)

3. Targeting Specific Tabs

# Execute action on specific tab using execute_action
computer.execute_action({
    "type": "click",
    "x": 100,
    "y": 200,
    "tab_id": "tab_abc123"
})

computer.execute_action({
    "type": "type",
    "text": "text",
    "tab_id": "tab_abc123"
})

computer.execute_action({
    "type": "screenshot",
    "tab_id": "tab_abc123"
})

Best Practices

Always store tab IDs in variables or dictionaries to avoid confusion. Losing track of tab IDs makes it impossible to target specific tabs.
Before scrolling or clicking, get page context to understand viewport dimensions and scroll position. This prevents out-of-bounds errors.
Close tabs you no longer need to free up browser resources. Each tab consumes memory and CPU.
Periodically call list_tabs to verify which tabs are open and their current state, especially in complex workflows.
Page context includes the current URL, so you can verify navigation succeeded or detect redirects.

Variations

Comparison Shopping

products = ["laptop dell xps", "laptop macbook pro", "laptop thinkpad"]

for product in products:
    result = computer.execute_action({
        "type": "new_tab",
        "url": f"https://shopping-site.com/search?q={product}"
    })
    tab_id = result.executed_tab_id

    # Extract price, take screenshot, etc.

Dashboard Monitoring

dashboards = [
    "https://grafana.example.com/dashboard/1",
    "https://grafana.example.com/dashboard/2",
    "https://datadog.example.com/dashboard/3"
]

for url in dashboards:
    result = computer.execute_action({"type": "new_tab", "url": url})
    computer.wait(3)  # Let metrics load

    screenshot = computer.execute_action({
        "type": "screenshot",
        "tab_id": result.executed_tab_id
    })
    # Save screenshot for monitoring

Next Steps