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.
from tzafon import Computer# Topics to researchtopics = ["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}")