-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathservice.py
More file actions
326 lines (289 loc) · 12.6 KB
/
service.py
File metadata and controls
326 lines (289 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import asyncio
import logging
from browser_use import Browser
from browser_use.agent.views import ActionResult
from browser_use.controller.service import Controller
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.prompts import PromptTemplate
from workflow_use.controller.utils import get_best_element_handle, truncate_selector
from workflow_use.controller.views import (
ClickElementDeterministicAction,
InputTextDeterministicAction,
KeyPressDeterministicAction,
NavigationAction,
PageExtractionAction,
ScrollDeterministicAction,
SelectDropdownOptionDeterministicAction,
)
logger = logging.getLogger(__name__)
DEFAULT_ACTION_TIMEOUT_MS = 2500
# List of default actions from browser_use.controller.service.Controller to disable
# todo: come up with a better way to filter out the actions (filter IN the actions would be much nicer in this case)
DISABLED_DEFAULT_ACTIONS = [
'done',
'search_google',
'go_to_url', # I am using this action from the main controller to avoid duplication
'go_back',
'wait',
'click_element_by_index',
'input_text',
'save_pdf',
'switch_tab',
'open_tab',
'close_tab',
'extract_content',
'scroll_down',
'scroll_up',
'send_keys',
'scroll_to_text',
'get_dropdown_options',
'select_dropdown_option',
'drag_drop',
'get_sheet_contents',
'select_cell_or_range',
'get_range_contents',
'clear_selected_range',
'input_selected_cell_text',
'update_range_contents',
]
class WorkflowController(Controller):
def __init__(self, *args, **kwargs):
# Pass the list of actions to exclude to the base class constructor
super().__init__(*args, exclude_actions=DISABLED_DEFAULT_ACTIONS, **kwargs)
self.__register_actions()
def __register_actions(self):
# Navigate to URL ------------------------------------------------------------
@self.registry.action('Manually navigate to URL', param_model=NavigationAction)
async def navigation(params: NavigationAction, browser_session: Browser) -> ActionResult:
"""Navigate to the given URL."""
page = await browser_session.get_current_page()
await page.goto(params.url)
await page.wait_for_load_state()
msg = f'🔗 Navigated to URL: {params.url}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
# Click element by CSS selector --------------------------------------------------
@self.registry.action(
'Click element by all available selectors',
param_model=ClickElementDeterministicAction,
)
async def click(params: ClickElementDeterministicAction, browser_session: Browser) -> ActionResult:
"""Click the first element matching *params.cssSelector* with fallback mechanisms."""
page = await browser_session.get_current_page()
original_selector = params.cssSelector
# If frameUrl or frameIdPath are provided, narrow the search to that frame
def _select_context(pg):
try:
from playwright.async_api import Page, Frame
ctx: Page | Frame = pg
# If frame hints point to top document, stay on page
fid = getattr(params, 'frameIdPath', None)
furl = getattr(params, 'frameUrl', None)
curr_url = (pg.url or '').split('#')[0] if hasattr(pg, 'url') else ''
if furl and curr_url and furl.split('#')[0] == curr_url:
return pg
if fid:
segs = [s for s in str(fid).split('.') if s != '']
if all(s == '0' for s in segs):
return pg
f = pg.main_frame
for s in segs[1:]: # skip top marker
idx = int(s)
if 0 <= idx < len(f.child_frames):
f = f.child_frames[idx]
else:
return pg
return f
if furl:
from urllib.parse import urlparse
pf = urlparse(furl)
# If frameUrl equals current page URL (origin+path), stay on page
try:
from urllib.parse import urlparse as _u
cu = _u(curr_url)
if (cu.scheme, cu.netloc, cu.path) == (pf.scheme, pf.netloc, pf.path):
return pg
except Exception:
pass
for fr in pg.frames:
try:
ff = urlparse(fr.url)
if (ff.scheme, ff.netloc) == (pf.scheme, pf.netloc) and fr.url.startswith(furl):
return fr
except Exception:
continue
except Exception:
return pg
return ctx
# Fallback: search all frames for selector (prefer frames matching target origin)
async def _find_in_frames(pg, selector: str):
from urllib.parse import urlparse
prefer = getattr(params, 'frameUrl', None) or getattr(params, 'url', None) or ''
pref_o = urlparse(prefer) if prefer else None
frames = list(pg.frames)
def score(fr):
if not pref_o:
return 0
try:
fo = urlparse(fr.url)
return 2 if (fo.scheme, fo.netloc) == (pref_o.scheme, pref_o.netloc) else 0
except Exception:
return 0
frames.sort(key=score, reverse=True)
for fr in frames:
try:
loc, used = await get_best_element_handle(fr, selector, params, timeout_ms=max(800, DEFAULT_ACTION_TIMEOUT_MS // 2))
return fr, loc, used
except Exception:
continue
return None, None, None
try:
# Only auto-navigate for top-document clicks (no frame hints) when a different URL is declared
curr = (page.url or '').split('#')[0]
declared_url = (getattr(params, 'url', None) or '').split('#')[0]
has_frame_hints = bool(getattr(params, 'frameIdPath', None) or getattr(params, 'frameUrl', None))
if declared_url and declared_url.startswith('http') and not has_frame_hints and curr != declared_url:
await page.goto(declared_url)
await page.wait_for_load_state()
ctx = _select_context(page)
try:
locator, selector_used = await get_best_element_handle(
ctx,
params.cssSelector,
params,
timeout_ms=DEFAULT_ACTION_TIMEOUT_MS,
)
except Exception:
# Fallback: search all frames
fr, locator, selector_used = await _find_in_frames(page, params.cssSelector)
if locator is None:
raise
await locator.click(force=True)
used_str = selector_used if isinstance(selector_used, str) and selector_used else params.cssSelector
msg = f'🖱️ Clicked element with CSS selector: {truncate_selector(used_str)} (original: {truncate_selector(original_selector)})'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
except Exception as e:
error_msg = f'Failed to click element. Original selector: {truncate_selector(original_selector)}. Error: {str(e)}'
logger.error(error_msg)
raise Exception(error_msg)
# Input text into element --------------------------------------------------------
@self.registry.action(
'Input text into an element by all available selectors',
param_model=InputTextDeterministicAction,
)
async def input(
params: InputTextDeterministicAction,
browser_session: Browser,
has_sensitive_data: bool = False,
) -> ActionResult:
"""Fill text into the element located with *params.cssSelector*."""
page = await browser_session.get_current_page()
original_selector = params.cssSelector
try:
locator, selector_used = await get_best_element_handle(
page,
params.cssSelector,
params,
timeout_ms=DEFAULT_ACTION_TIMEOUT_MS,
)
# Check if it's a SELECT element
is_select = await locator.evaluate('(el) => el.tagName === "SELECT"')
if is_select:
return ActionResult(
extracted_content='Ignored input into select element',
include_in_memory=True,
)
# Add a small delay and click to ensure the element is focused
await locator.fill(params.value)
await asyncio.sleep(0.5)
await locator.click(force=True)
await asyncio.sleep(0.5)
msg = f'⌨️ Input "{params.value}" into element with CSS selector: {truncate_selector(selector_used)} (original: {truncate_selector(original_selector)})'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
except Exception as e:
error_msg = f'Failed to input text. Original selector: {truncate_selector(original_selector)}. Error: {str(e)}'
logger.error(error_msg)
raise Exception(error_msg)
# Select dropdown option ---------------------------------------------------------
@self.registry.action(
'Select dropdown option by all available selectors and visible text',
param_model=SelectDropdownOptionDeterministicAction,
)
async def select_change(params: SelectDropdownOptionDeterministicAction, browser_session: Browser) -> ActionResult:
"""Select dropdown option whose visible text equals *params.value*."""
page = await browser_session.get_current_page()
original_selector = params.cssSelector
try:
locator, selector_used = await get_best_element_handle(
page,
params.cssSelector,
params,
timeout_ms=DEFAULT_ACTION_TIMEOUT_MS,
)
await locator.select_option(label=params.selectedText)
msg = f'Selected option "{params.selectedText}" in dropdown {truncate_selector(selector_used)} (original: {truncate_selector(original_selector)})'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
except Exception as e:
error_msg = f'Failed to select option. Original selector: {truncate_selector(original_selector)}. Error: {str(e)}'
logger.error(error_msg)
raise Exception(error_msg)
# Key press action ------------------------------------------------------------
@self.registry.action(
'Press key on element by all available selectors',
param_model=KeyPressDeterministicAction,
)
async def key_press(params: KeyPressDeterministicAction, browser_session: Browser) -> ActionResult:
"""Press *params.key* on the element identified by *params.cssSelector*."""
page = await browser_session.get_current_page()
original_selector = params.cssSelector
try:
locator, selector_used = await get_best_element_handle(page, params.cssSelector, params, timeout_ms=5000)
await locator.press(params.key)
msg = f"🔑 Pressed key '{params.key}' on element with CSS selector: {truncate_selector(selector_used)} (original: {truncate_selector(original_selector)})"
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
except Exception as e:
error_msg = f'Failed to press key. Original selector: {truncate_selector(original_selector)}. Error: {str(e)}'
logger.error(error_msg)
raise Exception(error_msg)
# Scroll action --------------------------------------------------------------
@self.registry.action('Scroll page', param_model=ScrollDeterministicAction)
async def scroll(params: ScrollDeterministicAction, browser_session: Browser) -> ActionResult:
"""Scroll the page by the given x/y pixel offsets."""
page = await browser_session.get_current_page()
await page.evaluate(f'window.scrollBy({params.scrollX}, {params.scrollY});')
msg = f'📜 Scrolled page by (x={params.scrollX}, y={params.scrollY})'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
# Extract content ------------------------------------------------------------
@self.registry.action(
'Extract page content to retrieve specific information from the page, e.g. all company names, a specific description, all information about, links with companies in structured format or simply links',
param_model=PageExtractionAction,
)
async def extract_page_content(
params: PageExtractionAction, browser_session: Browser, page_extraction_llm: BaseChatModel
):
page = await browser_session.get_current_page()
import markdownify
strip = ['a', 'img']
content = markdownify.markdownify(await page.content(), strip=strip)
# manually append iframe text into the content so it's readable by the LLM (includes cross-origin iframes)
for iframe in page.frames:
if iframe.url != page.url and not iframe.url.startswith('data:'):
content += f'\n\nIFRAME {iframe.url}:\n'
content += markdownify.markdownify(await iframe.content())
prompt = 'Your task is to extract the content of the page. You will be given a page and a goal and you should extract all relevant information around this goal from the page. If the goal is vague, summarize the page. Respond in json format. Extraction goal: {goal}, Page: {page}'
template = PromptTemplate(input_variables=['goal', 'page'], template=prompt)
try:
output = await page_extraction_llm.ainvoke(template.format(goal=params.goal, page=content))
msg = f'📄 Extracted from page\n: {output.content}\n'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
except Exception as e:
logger.debug(f'Error extracting content: {e}')
msg = f'📄 Extracted from page\n: {content}\n'
logger.info(msg)
return ActionResult(extracted_content=msg)