3
3
# @Author : wenshao
4
4
# @ProjectName: browser-use-webui
5
5
# @FileName: test_browser_use.py
6
- import pdb
7
6
8
7
from dotenv import load_dotenv
9
8
10
9
load_dotenv ()
11
10
import sys
12
11
13
12
sys .path .append ("." )
13
+ import asyncio
14
14
import os
15
15
import sys
16
16
from pprint import pprint
17
17
18
- import asyncio
19
18
from browser_use import Agent
20
19
from browser_use .agent .views import AgentHistoryList
21
20
25
24
async def test_browser_use_org ():
26
25
from browser_use .browser .browser import Browser , BrowserConfig
27
26
from browser_use .browser .context import (
28
- BrowserContext ,
29
27
BrowserContextConfig ,
30
28
BrowserContextWindowSize ,
31
29
)
30
+
32
31
llm = utils .get_llm_model (
33
32
provider = "azure_openai" ,
34
33
model_name = "gpt-4o" ,
35
34
temperature = 0.8 ,
36
35
base_url = os .getenv ("AZURE_OPENAI_ENDPOINT" , "" ),
37
- api_key = os .getenv ("AZURE_OPENAI_API_KEY" , "" )
36
+ api_key = os .getenv ("AZURE_OPENAI_API_KEY" , "" ),
38
37
)
39
38
40
39
window_w , window_h = 1920 , 1080
@@ -43,16 +42,18 @@ async def test_browser_use_org():
43
42
config = BrowserConfig (
44
43
headless = False ,
45
44
disable_security = True ,
46
- extra_chromium_args = [f' --window-size={ window_w } ,{ window_h } ' ],
45
+ extra_chromium_args = [f" --window-size={ window_w } ,{ window_h } " ],
47
46
)
48
47
)
49
48
async with await browser .new_context (
50
- config = BrowserContextConfig (
51
- trace_path = './tmp/traces' ,
52
- save_recording_path = "./tmp/record_videos" ,
53
- no_viewport = False ,
54
- browser_window_size = BrowserContextWindowSize (width = window_w , height = window_h ),
55
- )
49
+ config = BrowserContextConfig (
50
+ trace_path = "./tmp/traces" ,
51
+ save_recording_path = "./tmp/record_videos" ,
52
+ no_viewport = False ,
53
+ browser_window_size = BrowserContextWindowSize (
54
+ width = window_w , height = window_h
55
+ ),
56
+ )
56
57
) as browser_context :
57
58
agent = Agent (
58
59
task = "go to google.com and type 'OpenAI' click search and give me the first url" ,
@@ -61,32 +62,32 @@ async def test_browser_use_org():
61
62
)
62
63
history : AgentHistoryList = await agent .run (max_steps = 10 )
63
64
64
- print (' Final Result:' )
65
+ print (" Final Result:" )
65
66
pprint (history .final_result (), indent = 4 )
66
67
67
- print (' \n Errors:' )
68
+ print (" \n Errors:" )
68
69
pprint (history .errors (), indent = 4 )
69
70
70
71
# e.g. xPaths the model clicked on
71
- print (' \n Model Outputs:' )
72
+ print (" \n Model Outputs:" )
72
73
pprint (history .model_actions (), indent = 4 )
73
74
74
- print (' \n Thoughts:' )
75
+ print (" \n Thoughts:" )
75
76
pprint (history .model_thoughts (), indent = 4 )
76
77
# close browser
77
78
await browser .close ()
78
79
79
80
80
81
async def test_browser_use_custom ():
81
- from playwright .async_api import async_playwright
82
82
from browser_use .browser .context import BrowserContextWindowSize
83
+ from browser_use .browser .browser import BrowserConfig
84
+ from playwright .async_api import async_playwright
83
85
84
- from src .browser .custom_browser import CustomBrowser , BrowserConfig
85
- from src .browser .custom_context import BrowserContext , BrowserContextConfig
86
- from src .controller .custom_controller import CustomController
87
86
from src .agent .custom_agent import CustomAgent
88
87
from src .agent .custom_prompts import CustomSystemPrompt
89
- from src .browser .custom_context import CustomBrowserContext
88
+ from src .browser .custom_browser import CustomBrowser
89
+ from src .browser .custom_context import BrowserContextConfig
90
+ from src .controller .custom_controller import CustomController
90
91
91
92
window_w , window_h = 1920 , 1080
92
93
@@ -95,32 +96,32 @@ async def test_browser_use_custom():
95
96
# model_name="gpt-4o",
96
97
# temperature=0.8,
97
98
# base_url=os.getenv("AZURE_OPENAI_ENDPOINT", ""),
98
- # api_key=os.getenv("AZURE_OPENAI_API_KEY", "")
99
+ # api_key=os.getenv("AZURE_OPENAI_API_KEY", ""),
99
100
# )
100
101
101
- # llm = utils.get_llm_model(
102
- # provider="gemini",
103
- # model_name="gemini-2.0-flash-exp",
104
- # temperature=1.0,
105
- # api_key=os.getenv("GOOGLE_API_KEY", "")
106
- # )
102
+ llm = utils .get_llm_model (
103
+ provider = "gemini" ,
104
+ model_name = "gemini-2.0-flash-exp" ,
105
+ temperature = 1.0 ,
106
+ api_key = os .getenv ("GOOGLE_API_KEY" , "" )
107
+ )
107
108
108
109
# llm = utils.get_llm_model(
109
110
# provider="deepseek",
110
111
# model_name="deepseek-chat",
111
112
# temperature=0.8
112
113
# )
113
114
114
- llm = utils .get_llm_model (
115
- provider = "ollama" ,
116
- model_name = "qwen2.5:7b" ,
117
- temperature = 0.8
118
- )
115
+ # llm = utils.get_llm_model(
116
+ # provider="ollama", model_name="qwen2.5:7b", temperature=0.8
117
+ # )
119
118
120
119
controller = CustomController ()
121
120
use_own_browser = False
122
121
disable_security = True
123
- use_vision = False
122
+ use_vision = True # Set to False when using DeepSeek
123
+ tool_call_in_content = True # Set to True when using Ollama
124
+ max_actions_per_step = 1
124
125
playwright = None
125
126
browser_context_ = None
126
127
try :
@@ -134,14 +135,14 @@ async def test_browser_use_custom():
134
135
no_viewport = False ,
135
136
headless = False , # 保持浏览器窗口可见
136
137
user_agent = (
137
- ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
138
- ' (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
138
+ " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
139
+ " (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
139
140
),
140
141
java_script_enabled = True ,
141
142
bypass_csp = disable_security ,
142
143
ignore_https_errors = disable_security ,
143
144
record_video_dir = "./tmp/record_videos" ,
144
- record_video_size = {' width' : window_w , ' height' : window_h }
145
+ record_video_size = {" width" : window_w , " height" : window_h },
145
146
)
146
147
else :
147
148
browser_context_ = None
@@ -150,18 +151,20 @@ async def test_browser_use_custom():
150
151
config = BrowserConfig (
151
152
headless = False ,
152
153
disable_security = True ,
153
- extra_chromium_args = [f' --window-size={ window_w } ,{ window_h } ' ],
154
+ extra_chromium_args = [f" --window-size={ window_w } ,{ window_h } " ],
154
155
)
155
156
)
156
157
157
158
async with await browser .new_context (
158
- config = BrowserContextConfig (
159
- trace_path = './tmp/result_processing' ,
160
- save_recording_path = "./tmp/record_videos" ,
161
- no_viewport = False ,
162
- browser_window_size = BrowserContextWindowSize (width = window_w , height = window_h ),
159
+ config = BrowserContextConfig (
160
+ trace_path = "./tmp/result_processing" ,
161
+ save_recording_path = "./tmp/record_videos" ,
162
+ no_viewport = False ,
163
+ browser_window_size = BrowserContextWindowSize (
164
+ width = window_w , height = window_h
163
165
),
164
- context = browser_context_
166
+ ),
167
+ context = browser_context_ ,
165
168
) as browser_context :
166
169
agent = CustomAgent (
167
170
task = "go to google.com and type 'OpenAI' click search and give me the first url" ,
@@ -170,25 +173,28 @@ async def test_browser_use_custom():
170
173
browser_context = browser_context ,
171
174
controller = controller ,
172
175
system_prompt_class = CustomSystemPrompt ,
173
- use_vision = use_vision
176
+ use_vision = use_vision ,
177
+ tool_call_in_content = tool_call_in_content ,
178
+ max_actions_per_step = max_actions_per_step
174
179
)
175
180
history : AgentHistoryList = await agent .run (max_steps = 10 )
176
181
177
- print (' Final Result:' )
182
+ print (" Final Result:" )
178
183
pprint (history .final_result (), indent = 4 )
179
184
180
- print (' \n Errors:' )
185
+ print (" \n Errors:" )
181
186
pprint (history .errors (), indent = 4 )
182
187
183
188
# e.g. xPaths the model clicked on
184
- print (' \n Model Outputs:' )
189
+ print (" \n Model Outputs:" )
185
190
pprint (history .model_actions (), indent = 4 )
186
191
187
- print (' \n Thoughts:' )
192
+ print (" \n Thoughts:" )
188
193
pprint (history .model_thoughts (), indent = 4 )
189
194
# close browser
190
- except Exception as e :
195
+ except Exception :
191
196
import traceback
197
+
192
198
traceback .print_exc ()
193
199
finally :
194
200
# 显式关闭持久化上下文
@@ -202,6 +208,6 @@ async def test_browser_use_custom():
202
208
await browser .close ()
203
209
204
210
205
- if __name__ == ' __main__' :
211
+ if __name__ == " __main__" :
206
212
# asyncio.run(test_browser_use_org())
207
213
asyncio .run (test_browser_use_custom ())
0 commit comments