|
| 1 | +from typing import Dict |
| 2 | + |
1 | 3 | from selenium import webdriver
|
2 | 4 | from selenium.webdriver.remote.remote_connection import RemoteConnection
|
3 |
| -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
4 |
| -import requests |
| 5 | +from browserbase import Browserbase |
| 6 | +from dotenv import load_dotenv |
5 | 7 | import os
|
6 | 8 |
|
7 |
| -def create_session(): |
8 |
| - url = 'https://www.browserbase.com/v1/sessions' |
9 |
| - headers = {'Content-Type': 'application/json', 'x-bb-api-key': os.environ["BROWSERBASE_API_KEY"]} |
10 |
| - response = requests.post(url, json={ "projectId": os.environ["BROWSERBASE_PROJECT_ID"] }, headers=headers) |
11 |
| - return response.json()['id'] |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +BROWSERBASE_API_KEY = os.getenv("BROWSERBASE_API_KEY") |
| 12 | +BROWSERBASE_PROJECT_ID = os.getenv("BROWSERBASE_PROJECT_ID") |
| 13 | + |
| 14 | +bb = Browserbase(api_key=BROWSERBASE_API_KEY) |
| 15 | + |
| 16 | + |
| 17 | +class BrowserbaseConnection(RemoteConnection): |
| 18 | + """ |
| 19 | + Manage a single session with Browserbase. |
| 20 | + """ |
| 21 | + |
| 22 | + session_id: str |
| 23 | + |
| 24 | + def __init__(self, session_id: str, *args, **kwargs): # type: ignore |
| 25 | + super().__init__(*args, **kwargs) # type: ignore |
| 26 | + self.session_id = session_id |
| 27 | + |
| 28 | + def get_remote_connection_headers( # type: ignore |
| 29 | + self, parsed_url: str, keep_alive: bool = False |
| 30 | + ) -> Dict[str, str]: |
| 31 | + headers = super().get_remote_connection_headers(parsed_url, keep_alive) # type: ignore |
| 32 | + |
| 33 | + # Update headers to include the Browserbase required information |
| 34 | + headers["x-bb-api-key"] = BROWSERBASE_API_KEY |
| 35 | + headers["session-id"] = self.session_id |
| 36 | + |
| 37 | + return headers # type: ignore |
12 | 38 |
|
13 | 39 |
|
14 |
| -class CustomRemoteConnection(RemoteConnection): |
15 |
| - _session_id = None |
| 40 | +def run() -> None: |
| 41 | + # Use the custom class to create and connect to a new browser session |
| 42 | + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) |
| 43 | + connection = BrowserbaseConnection(session.id, session.selenium_remote_url) |
| 44 | + driver = webdriver.Remote( |
| 45 | + command_executor=connection, options=webdriver.ChromeOptions() # type: ignore |
| 46 | + ) |
16 | 47 |
|
17 |
| - def __init__(self, remote_server_addr: str, session_id: str): |
18 |
| - super().__init__(remote_server_addr) |
19 |
| - self._session_id = session_id |
| 48 | + # Print a bit of info about the browser we've connected to |
| 49 | + print( |
| 50 | + "Connected to Browserbase", |
| 51 | + f"{driver.name} version {driver.caps['browserVersion']}", # type: ignore |
| 52 | + ) |
20 | 53 |
|
21 |
| - def get_remote_connection_headers(self, parsed_url, keep_alive=False): |
22 |
| - headers = super().get_remote_connection_headers(parsed_url, keep_alive) |
23 |
| - headers.update({'x-bb-api-key': os.environ["BROWSERBASE_API_KEY"]}) |
24 |
| - headers.update({'session-id': self._session_id}) |
25 |
| - return headers |
| 54 | + try: |
| 55 | + # Perform our browser commands |
| 56 | + driver.get("https://www.sfmoma.org") |
| 57 | + print(f"At URL: {driver.current_url} | Title: {driver.title}") |
| 58 | + assert driver.current_url == "https://www.sfmoma.org/" |
| 59 | + assert driver.title == "SFMOMA" |
26 | 60 |
|
| 61 | + finally: |
| 62 | + # Make sure to quit the driver so your session is ended! |
| 63 | + driver.quit() |
27 | 64 |
|
28 |
| -def run(): |
29 |
| - session_id = create_session() |
30 |
| - custom_conn = CustomRemoteConnection('http://connect.browserbase.com/webdriver', session_id) |
31 |
| - options = webdriver.ChromeOptions() |
32 |
| - options.debugger_address = "localhost:9223" |
33 |
| - driver = webdriver.Remote(custom_conn, options=options) |
34 |
| - driver.get("https://www.browserbase.com") |
35 |
| - get_title = driver.title |
36 |
| - print(get_title) |
37 |
| - # Make sure to quit the driver so your session is ended! |
38 |
| - driver.quit() |
39 | 65 |
|
40 |
| -run() |
| 66 | +if __name__ == "__main__": |
| 67 | + run() |
0 commit comments