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: computer_id = computer.id # Navigate main tab to Wikipedia computer.navigate("https://wikipedia.org") computer.wait(2) # Get the main tab ID result = client.computers.tabs.list(computer_id) main_tab_id = result.result["tabs"][0]["tab_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 = client.computers.tabs.create( computer_id, url=f"https://en.wikipedia.org/wiki/{topic.replace(' ', '_')}" ) tab_id = result.result["created_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 (note: tab_id first, then id=computer_id) client.computers.tabs.switch(tab_id, id=computer_id) # Get page context with screenshot result = client.computers.screenshot( computer_id, 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 client.computers.scroll( computer_id, dx=0, dy=500, tab_id=tab_id ) computer.wait(1) # Take another screenshot after scrolling result = client.computers.screenshot( computer_id, 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 = client.computers.tabs.list(computer_id) for tab in result.result["tabs"]: active = "✓" if tab["is_main"] else " " print(f"[{active}] {tab['tab_id']}: {tab['url']}") # Close all topic tabs, keep only main print("\n--- Cleaning Up ---\n") for topic, tab_id in topic_tabs.items(): if topic != "Main": client.computers.tabs.delete(tab_id, id=computer_id) print(f"Closed tab: {topic}") # Verify only main tab remains result = client.computers.tabs.list(computer_id) remaining = len(result.result["tabs"]) print(f"\nTabs remaining: {remaining}")
# Execute actions on specific tab using client.computers APIclient.computers.click(computer_id, x=100, y=200, tab_id="tab_abc123")client.computers.type(computer_id, text="text", tab_id="tab_abc123")client.computers.screenshot(computer_id, tab_id="tab_abc123")