Skip to content

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

refactor: update agent focus handling and CDP session retrieval to fix tests

reformedot97deba7bb8bf
diff --git a/docs/customize/hooks.mdx b/docs/customize/hooks.mdx
index ea73dda2b..7e94e888b 100644
--- a/docs/customize/hooks.mdx
+++ b/docs/customize/hooks.mdx
@@ -104,7 +104,7 @@ When working with agent hooks, you have access to the entire `Agent` instance. H
- `agent.history.extracted_content()`: Content extracted from web pages
- `agent.history.urls()`: URLs visited by the agent
- `agent.browser_session` gives direct access to the `BrowserSession` and CDP interface
- - `agent.browser_session.agent_focus`: Get the current CDP session the agent is focused on
+ - `agent.browser_session.agent_focus_target_id`: Get the current target ID the agent is focused on
- `agent.browser_session.get_or_create_cdp_session()`: Get the current CDP session for browser interaction
- `agent.browser_session.get_tabs()`: Get all tabs currently open
- `agent.browser_session.get_current_page_url()`: Get the URL of the current active tab
diff --git a/tests/ci/browser/test_dom_serializer.py b/tests/ci/browser/test_dom_serializer.py
index 18b4262c2..e91c9a33e 100644
--- a/tests/ci/browser/test_dom_serializer.py
+++ b/tests/ci/browser/test_dom_serializer.py
@@ -240,11 +240,11 @@ class TestDOMSerializer:
print('\n✅ Validating click counter...')
# Get the CDP session for the main page (use target from a regular DOM element)
- # Note: browser_session.agent_focus may point to a different target than the page
+ # Note: browser_session.agent_focus_target_id may point to a different target than the page
if regular_elements and regular_elements[0][1].target_id:
cdp_session = await browser_session.get_or_create_cdp_session(target_id=regular_elements[0][1].target_id)
else:
- cdp_session = browser_session.agent_focus
+ cdp_session = await browser_session.get_or_create_cdp_session()
result = await cdp_session.cdp_client.send.Runtime.evaluate(
params={
@@ -498,7 +498,7 @@ class TestDOMSerializer:
elif final_button:
cdp_session = await browser_session.get_or_create_cdp_session(target_id=final_button[1].target_id)
else:
- cdp_session = browser_session.agent_focus
+ cdp_session = await browser_session.get_or_create_cdp_session()
result = await cdp_session.cdp_client.send.Runtime.evaluate(
params={
diff --git a/tests/ci/test_tools.py b/tests/ci/test_tools.py
index d6fa85a40..c6f76dc5e 100644
--- a/tests/ci/test_tools.py
+++ b/tests/ci/test_tools.py
@@ -333,7 +333,7 @@ class TestToolsIntegration:
await tools.navigate(url=f'{base_url}/dropdown1', new_tab=False, browser_session=browser_session)
# Wait for the page to load using CDP
- cdp_session = browser_session.agent_focus
+ cdp_session = await browser_session.get_or_create_cdp_session()
assert cdp_session is not None, 'CDP session not initialized'
# Wait for page load by checking document ready state
@@ -445,7 +445,7 @@ class TestToolsIntegration:
await tools.navigate(url=f'{base_url}/dropdown2', new_tab=False, browser_session=browser_session)
# Wait for the page to load using CDP
- cdp_session = browser_session.agent_focus
+ cdp_session = await browser_session.get_or_create_cdp_session()
assert cdp_session is not None, 'CDP session not initialized'
# Wait for page load by checking document ready state