Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Preserve page structure and bound OpenAI observations
Magnus Muellere3e1fc8d5d63
diff --git a/browser_use/integrations/openai/browser_use.py b/browser_use/integrations/openai/browser_use.pyindex e6fd72539..4a962f71c 100644--- a/browser_use/integrations/openai/browser_use.py+++ b/browser_use/integrations/openai/browser_use.py@@ -168,8 +168,22 @@ class BrowserUse:'listbox','switch',}- lines = []- for node in result['nodes']:+ lines: list[str] = []+ nodes = {node['nodeId']: node for node in result['nodes']}+ children = {child for node in result['nodes'] for child in node.get('childIds', [])}+ pending = [(node, 0) for node in reversed(result['nodes']) if node['nodeId'] not in children]+ seen: set[str] = set()+ length = 0+ notice = '\n[Page truncated; use read_page_scoped, find, or interactive reads for specific content.]'+ while pending:+ node, depth = pending.pop()+ if node['nodeId'] in seen:+ continue+ seen.add(node['nodeId'])+ visible = not node.get('ignored') and node.get('role', {}).get('value') != 'InlineTextBox'+ pending.extend(+ (nodes[child], min(depth + int(visible), 30)) for child in reversed(node.get('childIds', [])) if child in nodes+ )role = node.get('role', {}).get('value', '')if node.get('ignored') or role == 'InlineTextBox':continue@@ -186,10 +200,21 @@ class BrowserUse:states = [f'{prop["name"]}={json.dumps(prop["value"].get("value"))}'for prop in node.get('properties', [])- if prop['name'] in {'checked', 'selected'} and 'value' in prop.get('value', {})+ if prop['name']+ in {'checked', 'selected', 'expanded', 'disabled', 'required', 'level', 'hasPopup', 'multiselectable'}+ and 'value' in prop.get('value', {})]+ value = node.get('value', {}).get('value')+ if value is not None:+ states.append(f'value={json.dumps(value, ensure_ascii=False)}')suffix = ' ' + ' '.join(states) if states else ''- lines.append(f'{prefix}{role} {json.dumps(name, ensure_ascii=False)}{suffix}')+ line = f'{" " * depth}{prefix}{role} {json.dumps(name, ensure_ascii=False)}{suffix}'+ 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) + 1return '\n'.join(lines)async def find(self, args: api.FindInput) -> str:diff --git a/tests/ci/integrations/test_openai_browser_use.py b/tests/ci/integrations/test_openai_browser_use.pyindex 28595320a..dff062c84 100644--- a/tests/ci/integrations/test_openai_browser_use.py+++ b/tests/ci/integrations/test_openai_browser_use.py@@ -352,3 +352,27 @@ async def test_network_diagnostics_strip_query_and_capture_status(driver):assert '/diagnostic' in read['text']assert 'secret' not in read['text']assert 'hidden' not in read['text']+++async def test_page_read_preserves_structure_and_control_state(driver):+ await fixture_page(+ driver,+ '<main><h1>Account</h1><section aria-label="Preferences"><label>Name<input value="Alice"></label><button disabled>Save</button><button aria-expanded="false">Details</button></section></main>',+ )+ text = await driver.read_page({'tab_id': None, 'filter': 'all'})+ lines = text.splitlines()+ main = next(line for line in lines if 'main ' in line)+ heading = next(line for line in lines if 'heading "Account"' in line)+ assert len(heading) - len(heading.lstrip()) > len(main) - len(main.lstrip())+ assert 'value="Alice"' in next(line for line in lines if 'textbox' in line)+ assert 'disabled=true' in next(line for line in lines if 'Save' in line)+ assert 'expanded=false' in next(line for line in lines if 'Details' in line)+++async def test_page_read_bounds_large_observations(driver):+ await fixture_page(driver, '<main><p>' + 'large content ' * 5000 + '</p><button>Last</button></main>')+ text = await driver.read_page({'tab_id': None, 'filter': 'all'})+ assert len(text) <= 50000+ assert 'truncated' in text.lower()+ controls = await driver.read_page({'tab_id': None, 'filter': 'interactive'})+ await driver.click({'tab_id': None, 'target': reference(controls, 'Last')})