Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

include-js-tool

Magnus Müllerb208312ceb3d
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 9c62dcc8d..08be7ae0a 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -37,6 +37,7 @@ from browser_use.tools.views import (
ClickElementAction,
CloseTabAction,
DoneAction,
+ ExecuteCDPAction,
GetDropdownOptionsAction,
GoToUrlAction,
InputTextAction,
@@ -892,6 +893,35 @@ You will be given a query and the markdown of a webpage that has been filtered t
include_extracted_content_only_once=True,
)
+ # General CDP execution tool
+
+ @self.registry.action(
+ """This JavaScript code gets executed with Runtime.evaluate
+ - Write code to solve problems you could not solve with other tools.
+ - Don't write comments in here, no human reads that.
+ - Write only valid code.
+EXAMPLES:
+Clicking on coordinates, using when other tools fail, filling a form all at once, hovering, dragging, extracting only links, extracting query, zooming ....
+You can also use it to explore the website.
+""",
+ param_model=ExecuteCDPAction,
+ )
+ async def execute_js(params: ExecuteCDPAction, browser_session: BrowserSession):
+ # Pre-process JavaScript to fix common issues
+ code = params.js_code
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ try:
+ result = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': code}, session_id=cdp_session.session_id
+ )
+ result_text = result.get('result', {}).get('value', '')
+ description = result.get('result', {}).get('description', '')
+ error_message = result.get('exceptionDetails', {}).get('text', '')
+ return ActionResult(extracted_content=result_text)
+
+ except Exception as e:
+ return ActionResult(error=f'Failed to execute JavaScript: {e}')
+
# Custom done action for structured output
async def extract_clean_markdown(
self, browser_session: BrowserSession, extract_links: bool = False
diff --git a/browser_use/tools/views.py b/browser_use/tools/views.py
index 0a1c00827..b472be193 100644
--- a/browser_use/tools/views.py
+++ b/browser_use/tools/views.py
@@ -91,3 +91,7 @@ class GetDropdownOptionsAction(BaseModel):
class SelectDropdownOptionAction(BaseModel):
index: int = Field(ge=1, description='index of the dropdown element to select an option for')
text: str = Field(description='the text or exact value of the option to select')
+
+
+class ExecuteCDPAction(BaseModel):
+ js_code: str = Field(description='JavaScript code to execute via CDP Runtime.evaluate')