To use Hyperbrowser with your code, you will need an API Key. You can get one easily from the dashboard. Once you have your API Key, add it to your .env file as HYPERBROWSER_API_KEY .
3
Setup Browser Session
Next, you can easily startup a browser session using playwright and your Hyperbrowser API Key.
import { chromium } from"playwright-core";import { config } from"dotenv";config();constmain=async () => {// Connect to browser using Playwrightconstbrowser=awaitchromium.connectOverCDP(`wss://connect.hyperbrowser.ai?apiKey=${process.env.HYPERBROWSER_API_KEY}` );// Create a new context and pageconstdefaultContext=browser.contexts()[0];constpage=defaultContext.pages()[0];// Navigate to a websiteconsole.log("Navigating to Hacker News...");awaitpage.goto("https://news.ycombinator.com/");constpageTitle=awaitpage.title();console.log("Page 1:", pageTitle);awaitpage.evaluate(() => {console.log("Page 1:",document.title); });awaitpage.goto("https://example.com");console.log("Page 2:",awaitpage.title());awaitpage.evaluate(() => {console.log("Page 2:",document.title); });awaitpage.goto("https://apple.com");console.log("Page 3:",awaitpage.title());awaitpage.evaluate(() => {console.log("Page 3:",document.title); });awaitpage.goto("https://google.com");console.log("Page 4:",awaitpage.title());awaitpage.evaluate(() => {console.log("Page 4:",document.title); });// Clean upawaitpage.close();awaitdefaultContext.close();awaitbrowser.close();};main();
import osfrom playwright.sync_api import sync_playwrightfrom dotenv import load_dotenv# Load environment variables from .env fileload_dotenv()defmain():withsync_playwright()as p:# Connect to browser using Playwright browser = p.chromium.connect_over_cdp(f"wss://connect.hyperbrowser.ai?apiKey={os.getenv('HYPERBROWSER_API_KEY')}" )# Get the default context and page default_context = browser.contexts[0] page = default_context.pages[0]# Navigate to various websitesprint("Navigating to Hacker News...") page.goto("https://news.ycombinator.com/") page_title = page.title()print("Page 1:", page_title) page.evaluate("() => { console.log('Page 1:', document.title); }") page.goto("https://example.com")print("Page 2:", page.title()) page.evaluate("() => { console.log('Page 2:', document.title); }") page.goto("https://apple.com")print("Page 3:", page.title()) page.evaluate("() => { console.log('Page 3:', document.title); }") page.goto("https://google.com")print("Page 4:", page.title()) page.evaluate("() => { console.log('Page 4:', document.title); }")# Clean up page.close() default_context.close() browser.close()if__name__=="__main__":main()
4
View Session in Dashboard
You can view all your sessions in the dashboard and see their recordings or other key metrics like logs.