MCPcopy
hub / github.com/browser-use/browser-use / capture_dom_state

Function capture_dom_state

tests/scripts/debug_iframe_scrolling.py:130–166  ·  view source on GitHub ↗

Capture DOM state and return analysis

(label: str)

Source from the content-addressed store, hash-verified

128
129 # Helper function to capture and analyze DOM state
130 async def capture_dom_state(label: str) -> dict:
131 """Capture DOM state and return analysis"""
132 print(f'\n📸 Capturing DOM state: {label}')
133 state_event = browser_session.event_bus.dispatch(
134 BrowserStateRequestEvent(include_dom=True, include_screenshot=False, include_recent_events=False)
135 )
136 browser_state = await state_event.event_result()
137
138 if browser_state and browser_state.dom_state and browser_state.dom_state.selector_map:
139 selector_map = browser_state.dom_state.selector_map
140 element_count = len(selector_map)
141
142 # Check for specific elements
143 found_elements = {}
144 expected_checks = [
145 ('First Name', ['firstName', 'first name']),
146 ('Last Name', ['lastName', 'last name']),
147 ('Email', ['email']),
148 ('City', ['city']),
149 ('State', ['state']),
150 ('Zip', ['zip', 'zipCode']),
151 ]
152
153 for name, keywords in expected_checks:
154 for index, element in selector_map.items():
155 element_str = str(element).lower()
156 if any(kw.lower() in element_str for kw in keywords):
157 found_elements[name] = True
158 break
159
160 return {
161 'label': label,
162 'total_elements': element_count,
163 'found_elements': found_elements,
164 'selector_map': selector_map,
165 }
166 return {'label': label, 'error': 'No DOM state available'}
167
168 # Capture initial state before any actions
169 print('\n' + '=' * 80)

Callers 2

debug_iframe_scrollingFunction · 0.85
wrapped_actFunction · 0.85

Calls 2

dispatchMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…