11from examples import bb , BROWSERBASE_PROJECT_ID , BROWSERBASE_API_KEY
2- from browserbase .types import SessionCreateResponse
32from selenium import webdriver
4- from selenium .webdriver .remote .webdriver import WebDriver
53from selenium .webdriver .remote .remote_connection import RemoteConnection
4+ from typing import Dict
65
76
87class BrowserbaseConnection (RemoteConnection ):
98 """
109 Manage a single session with Browserbase.
1110 """
1211
13- browserbase_session : SessionCreateResponse
1412 session_id : str
1513
16- def __init__ (self , session_id : str , * args , ** kwargs ):
17- super ().__init__ (* args , ** kwargs )
14+ def __init__ (self , session_id : str , * args , ** kwargs ): # type: ignore
15+ super ().__init__ (* args , ** kwargs ) # type: ignore
1816 self .session_id = session_id
1917
20- def get_remote_connection_headers (self , parsed_url , keep_alive = False ):
21- headers = super ().get_remote_connection_headers (parsed_url , keep_alive )
18+ def get_remote_connection_headers ( # type: ignore
19+ self , parsed_url : str , keep_alive : bool = False
20+ ) -> Dict [str , str ]:
21+ headers = super ().get_remote_connection_headers (parsed_url , keep_alive ) # type: ignore
2222
2323 # Update headers to include the Browserbase required information
2424 headers ["x-bb-api-key" ] = BROWSERBASE_API_KEY
@@ -27,31 +27,32 @@ def get_remote_connection_headers(self, parsed_url, keep_alive=False):
2727 return headers
2828
2929
30- def run (driver : WebDriver ):
31- # Instruct the browser to go to the SF MOMA page
32- driver .get ("https://www.sfmoma.org" )
30+ def run ():
31+ # Use the custom class to create and connect to a new browser session
32+ session = bb .sessions .create (project_id = BROWSERBASE_PROJECT_ID )
33+ connection = BrowserbaseConnection (session .id , session .selenium_remote_url )
34+ driver = webdriver .Remote (
35+ command_executor = connection , options = webdriver .ChromeOptions () # type: ignore
36+ )
3337
34- # Print out a bit of info about the page it landed on
35- print (f"{ driver .current_url = } | { driver .title = } " )
38+ # Print a bit of info about the browser we've connected to
39+ print (
40+ "Connected to Browserbase" ,
41+ f"{ driver .name } version { driver .caps ['browserVersion' ]} " , # type: ignore
42+ )
3643
37- ...
44+ try :
45+ # Perform our browser commands
46+ driver .get ("https://www.sfmoma.org" )
47+ print (f"At URL: { driver .current_url } | Title: { driver .title } " )
48+ assert driver .current_url == "https://www.sfmoma.org/"
49+ assert driver .title == "SFMOMA"
3850
51+ finally :
52+ # Make sure to quit the driver so your session is ended!
53+ print ("Quitting driver" )
54+ # driver.quit()
3955
40- # Use the custom class to create and connect to a new browser session
41- session = bb .sessions .create (project_id = BROWSERBASE_PROJECT_ID )
42- connection = BrowserbaseConnection (session .id , session .selenium_remote_url )
43- driver = webdriver .Remote (connection , options = webdriver .ChromeOptions ())
4456
45- # Print a bit of info about the browser we've connected to
46- print (
47- "Connected to Browserbase" ,
48- f"{ driver .name } version { driver .caps ['browserVersion' ]} " ,
49- )
50-
51- try :
52- # Perform our browser commands
53- run (driver )
54-
55- finally :
56- # Make sure to quit the driver so your session is ended!
57- driver .quit ()
57+ if __name__ == "__main__" :
58+ run ()
0 commit comments