### Python + Playwright Example with Hyperbrowser Source: https://www.hyperbrowser.ai This snippet demonstrates how to use the Hyperbrowser Python SDK with Playwright to automate browser interactions. It includes session creation, browser connection via CDP, navigation, and session cleanup. Ensure your HYPERBROWSER_API_KEY is set in your environment variables. ```python # Python + Playwright Example from hyperbrowser import Hyperbrowser from playwright.sync_api import sync_playwright import os from dotenv import load_dotenv load_dotenv() client = Hyperbrowser(api_key=os.environ["HYPERBROWSER_API_KEY"]) def main(): # Create session session = client.sessions.create() try: with sync_playwright() as p: # Connect Playwright browser = p.chromium.connect_over_cdp(session.ws_endpoint) default_context = browser.contexts[0] # Get the default page page = default_context.pages[0] # Navigate and interact page.goto("https://example.com") print(f"Title: {page.title()}") except Exception as e: print(f"Encountered error: {e}") finally: # Clean up client.sessions.stop(session.id) main() ``` -------------------------------- ### Start and Wait for Data Extraction Source: https://www.hyperbrowser.ai Initiates a data extraction task and waits for the result. Requires an API key and specifies URLs, a prompt, and a schema for the extracted data. ```typescript import { Hyperbrowser } from "@hyperbrowser/sdk"; const client = new Hyperbrowser({ apiKey: "your-api-key" }); const result = await client.extract.startAndWait({ urls: ["https://example.com"], prompt: "Extract the main heading", schema: { type: "object", properties: { heading: { type: "string" }, description: { type: "string" }, }, }, }); console.log(result); ``` -------------------------------- ### Python Flight Scraper with Hyperbrowser and Playwright Source: https://www.hyperbrowser.ai This script demonstrates how to use Hyperbrowser to create a browser session, connect Playwright to it, navigate to a flight search page, fill in details, click search, and extract flight results. It includes session creation, error handling, and session cleanup. ```python from hyperbrowser import Hyperbrowser from playwright.sync_api import sync_playwright import os from dotenv import load_dotenv import json load_dotenv() client = Hyperbrowser(api_key=os.environ["HYPERBROWSER_API_KEY"]) def main(): # Create session session = client.sessions.create() try: with sync_playwright() as p: # Connect Playwright to Hyperbrowser browser = p.chromium.connect_over_cdp(session.ws_endpoint) default_context = browser.contexts[0] page = default_context.pages[0] # Navigate to flight search page.goto("https://www.google.com/travel/flights") # Fill in flight details page.fill('input[placeholder*="Where from"]', "New York") page.wait_for_timeout(500) page.fill('input[placeholder*="Where to"]', "San Francisco") page.wait_for_timeout(500) # Click search button page.click('button[aria-label*="Search"]') page.wait_for_load_state("networkidle") # Extract flight data flights = page.locator('.flight-result').all() results = [] for flight in flights[:5]: # Get top 5 results result = { "airline": flight.locator('.airline-name').inner_text(), "price": flight.locator('.price').inner_text(), "duration": flight.locator('.duration').inner_text() } results.append(result) print(json.dumps(results, indent=2)) except Exception as e: print(f"Encountered error: {e}") finally: # Clean up session client.sessions.stop(session.id) if __name__ == "__main__": main() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.