Setup your Environment
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
.
Setup Browser Session
Next, you can easily startup a browser session using selenium and your Hyperbrowser API Key.
Node Python
Copy import dotenv from 'dotenv' ;
import https from 'https' ;
import { Builder , WebDriver } from 'selenium-webdriver' ;
import { Options } from 'selenium-webdriver/chrome' ;
import { Hyperbrowser } from '@hyperbrowser/sdk' ;
// Load environment variables from .env file
dotenv .config ();
const client = new Hyperbrowser ({ apiKey : process . env . HYPERBROWSER_API_KEY as string });
async function main () {
const session = await client . sessions .create ();
const customHttpsAgent = new https .Agent ({});
(customHttpsAgent as any ). addRequest = (req : any , options : any ) => {
req .setHeader ( 'x-hyperbrowser-token' , session .token);
( https . Agent . prototype as any ). addRequest .call (customHttpsAgent , req , options);
};
const driver : WebDriver = await new Builder ()
.forBrowser ( 'chrome' )
.usingHttpAgent (customHttpsAgent)
.usingServer ( 'https://connect.hyperbrowser.ai/webdriver' )
.setChromeOptions ( new Options ())
.build ();
try {
// Navigate to a URL
await driver .get ( "https://www.google.com" );
console .log ( "Navigated to Google" );
// Search
const searchBox = await driver .findElement ({ name : "q" });
await searchBox .sendKeys ( "Selenium WebDriver" );
await searchBox .submit ();
console .log ( "Performed search" );
// Screenshot
await driver .takeScreenshot () .then (data => {
require ( 'fs' ) .writeFileSync ( 'search_results.png' , data , 'base64' );
});
console .log ( "Screenshot saved" );
} finally {
await driver .quit ();
}
}
if ( require .main === module ) {
main () .catch ( console .error);
}
Copy import os
from dotenv import load_dotenv
from selenium import webdriver
from selenium . webdriver . remote . remote_connection import RemoteConnection
from selenium . webdriver . chrome . options import Options
from hyperbrowser import Hyperbrowser
# Load environment variables from .env file
load_dotenv ()
client = Hyperbrowser (api_key = os. getenv ( "HYPERBROWSER_API_KEY" ))
class CustomRC ( RemoteConnection ):
_signing_key = None
def __init__ ( self , server : str , token : str ):
super (). __init__ (server)
self . _token = token
def get_remote_connection_headers ( self , parsed_url , keep_alive = False ):
headers = super (). get_remote_connection_headers (parsed_url, keep_alive)
headers . update ({ 'x-hyperbrowser-token' : self._token})
return headers
def main ():
session = client . sessions . create ()
custom_conn = CustomRC ( "https://connect.hyperbrowser.ai/webdriver" , session.token)
driver = webdriver . Remote (custom_conn, options = Options ())
# Navigate to a URL
driver . get ( "https://www.google.com" )
print ( "Navigated to Google" )
# Search
search_box = driver . find_element ( "name" , "q" )
search_box . send_keys ( "Selenium WebDriver" )
search_box . submit ()
print ( "Performed search" )
# Screenshot
driver . save_screenshot ( "search_results.png" )
print ( "Screenshot saved" )
if __name__ == "__main__" :
main ()
View Session in Dashboard
You can view all your sessions in the dashboard and see their recordings or other key metrics like logs.