@@ -35,43 +35,32 @@ python examples/full_example.py
3535This example demonstrates the full Stagehand workflow: starting a session, navigating to a page, observing possible actions, acting on elements, extracting data, and running an autonomous agent.
3636
3737``` python
38- from stagehand import Stagehand, __version__
38+ from stagehand import Stagehand
3939
4040
4141def main () -> None :
42- sdk_version = __version__
43-
4442 # Create client using environment variables:
4543 # BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY
4644 client = Stagehand()
4745
48- # Start a new browser session
49- start_response = client.sessions.start (
46+ # Start a new browser session (returns a session helper bound to a session_id)
47+ session = client.sessions.create (
5048 model_name = " openai/gpt-5-nano" ,
51- x_language = " python" ,
52- x_sdk_version = sdk_version,
5349 )
5450
55- session_id = start_response.data.session_id
56- print (f " Session started: { session_id} " )
51+ print (f " Session started: { session.id} " )
5752
5853 try :
5954 # Navigate to a webpage
60- client.sessions.navigate(
61- id = session_id,
55+ session.navigate(
6256 url = " https://news.ycombinator.com" ,
6357 frame_id = " " , # empty string for the main frame
64- x_language = " python" ,
65- x_sdk_version = sdk_version,
6658 )
6759 print (" Navigated to Hacker News" )
6860
6961 # Observe to find possible actions on the page
70- observe_response = client.sessions.observe(
71- id = session_id,
62+ observe_response = session.observe(
7263 instruction = " find the link to view comments for the top post" ,
73- x_language = " python" ,
74- x_sdk_version = sdk_version,
7564 )
7665
7766 results = observe_response.data.result
@@ -83,17 +72,13 @@ def main() -> None:
8372 action = results[0 ].to_dict(exclude_none = True )
8473 print (" Acting on:" , action.get(" description" ))
8574
86- act_response = client.sessions.act(
87- id = session_id,
75+ act_response = session.act(
8876 input = action,
89- x_language = " python" ,
90- x_sdk_version = sdk_version,
9177 )
9278 print (" Act completed:" , act_response.data.result.message)
9379
9480 # Extract structured data from the page using a JSON schema
95- extract_response = client.sessions.extract(
96- id = session_id,
81+ extract_response = session.extract(
9782 instruction = " extract the text of the top comment on this page" ,
9883 schema = {
9984 " type" : " object" ,
@@ -103,32 +88,27 @@ def main() -> None:
10388 },
10489 " required" : [" commentText" ],
10590 },
106- x_language = " python" ,
107- x_sdk_version = sdk_version,
10891 )
10992
11093 extracted = extract_response.data.result
11194 author = extracted.get(" author" , " unknown" ) if isinstance (extracted, dict ) else " unknown"
11295 print (" Extracted author:" , author)
11396
11497 # Run an autonomous agent to accomplish a complex task
115- execute_response = client.sessions.execute(
116- id = session_id,
98+ execute_response = session.execute(
11799 execute_options = {
118100 " instruction" : f " Find any personal website, GitHub, or LinkedIn profile for the Hacker News user ' { author} '. " ,
119101 " max_steps" : 10 ,
120102 },
121103 agent_config = {" model" : " openai/gpt-5-nano" },
122- x_language = " python" ,
123- x_sdk_version = sdk_version,
124104 timeout = 300.0 ,
125105 )
126106
127107 print (" Agent completed:" , execute_response.data.result.message)
128108 print (" Agent success:" , execute_response.data.result.success)
129109 finally :
130110 # End the browser session to clean up resources
131- client.sessions. end(id = session_id, x_language = " python " , x_sdk_version = sdk_version )
111+ session. end()
132112 print (" Session ended" )
133113
134114
0 commit comments