Skip to content

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

Add new action to retrieve accessibility tree from the current page

Magnus Müller9261112c5168
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index 08e2a6d4f..2199c413b 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -257,6 +257,28 @@ class Controller(Generic[Context]):
logger.info(msg)
return ActionResult(extracted_content=msg)
+ @self.registry.action(
+ 'Get the accessibility tree of the page in the format "role name" with the number_of_elements to return',
+ )
+ async def get_ax_tree(number_of_elements: int, browser_session: BrowserSession):
+ page = await browser_session.get_current_page()
+ node = await page.accessibility.snapshot(interesting_only=True)
+
+ def flatten_ax_tree(node, lines):
+ if not node:
+ return
+ role = node.get('role', '')
+ name = node.get('name', '')
+ lines.append(f'{role} {name}')
+ for child in node.get('children', []):
+ flatten_ax_tree(child, lines)
+
+ lines = []
+ flatten_ax_tree(node, lines)
+ msg = '\n'.join(lines)
+ logger.info(msg)
+ return ActionResult(extracted_content=msg, include_in_memory=False)
+
@self.registry.action(
'Scroll down the page by pixel amount - if none is given, scroll one page',
param_model=ScrollAction,