Skip to content

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

Preserve find results beyond page observation limit

Magnus Mueller2b9d0aeaae3b
diff --git a/browser_use/integrations/openai/browser_use.py b/browser_use/integrations/openai/browser_use.py
index d667d8687..2b74c8cd3 100644
--- a/browser_use/integrations/openai/browser_use.py
+++ b/browser_use/integrations/openai/browser_use.py
@@ -155,6 +155,10 @@ class BrowserUse:
async def read_page(self, args: api.ReadPageInput) -> str:
"""Read the accessibility tree and issue tab-bound element references."""
+ return await self._read_page(args)
+
+ async def _read_page(self, args: api.ReadPageInput, query: str | None = None) -> str:
+ terms = query.casefold().split() if query else []
page = await self._page(args['tab_id'])
await self.browser.cdp_client.send.DOM.getDocument({'depth': 0}, session_id=await page.session_id)
self._refs = {name: ref for name, ref in self._refs.items() if ref.tab != page._target_id}
@@ -216,9 +220,10 @@ class BrowserUse:
states.append(f'value={json.dumps(value, ensure_ascii=False)}')
suffix = ' ' + ' '.join(states) if states else ''
line = f'{" " * depth}{prefix}{role} {json.dumps(name, ensure_ascii=False)}{suffix}'
+ if not all(term in line.casefold() for term in terms):
+ continue
remaining = 50000 - len(notice) - length
if len(line) + 1 > remaining:
- lines.append(line[: max(0, remaining - 1)])
return '\n'.join(lines) + notice
lines.append(line)
length += len(line) + 1
@@ -226,9 +231,7 @@ class BrowserUse:
async def find(self, args: api.FindInput) -> str:
"""Find current interactive elements by visible label text."""
- text = await self.read_page({'tab_id': args['tab_id'], 'filter': 'interactive'})
- terms = args['query'].casefold().split()
- return '\n'.join(line for line in text.splitlines() if all(term in line.casefold() for term in terms))
+ return await self._read_page({'tab_id': args['tab_id'], 'filter': 'interactive'}, query=args['query'])
async def get_page_text(self, args: api.TabInput) -> str:
"""Read the selected page without returning form values."""
diff --git a/tests/ci/integrations/test_openai_browser_use.py b/tests/ci/integrations/test_openai_browser_use.py
index dd9d6749d..5852aff3b 100644
--- a/tests/ci/integrations/test_openai_browser_use.py
+++ b/tests/ci/integrations/test_openai_browser_use.py
@@ -358,7 +358,14 @@ async def test_page_read_preserves_structure_and_control_state(driver):
async def test_page_read_bounds_large_observations(driver):
- await fixture_page(driver, '<main><p>' + 'large content ' * 5000 + '</p><button>Last</button></main>')
+ await fixture_page(
+ driver,
+ '<main><p>'
+ + 'large content ' * 5000
+ + '</p>'
+ + ('<button>' + 'filler ' * 100 + '</button>') * 100
+ + '<button>Last</button></main>',
+ )
text = await driver.read_page({'tab_id': None, 'filter': 'all'})
assert len(text) <= 50000
assert 'truncated' in text.lower()
@@ -369,5 +376,12 @@ async def test_page_read_bounds_large_observations(driver):
assert len(script) <= 50000
assert 'truncated' in script.lower()
assert await driver.run_script({'tab_id': None, 'source': '1 + 2', 'timeout_ms': 1000}) == '3'
- controls = await driver.read_page({'tab_id': None, 'filter': 'interactive'})
+ controls = await driver.find({'tab_id': None, 'query': 'Last'})
await driver.click({'tab_id': None, 'target': reference(controls, 'Last')})
+
+
+async def test_page_read_does_not_expose_password_value(driver):
+ await fixture_page(driver, '<label>Password<input type="password" value="private-password-value"></label>')
+ text = await driver.read_page({'tab_id': None, 'filter': 'all'})
+ assert 'private-password-value' not in text
+ assert 'Password' in text