Skip to content

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

fixed highlights iframe absolute positioning

Gregor Žunič3a8cf854edda
diff --git a/browser_use/dom/debug/highlights.py b/browser_use/dom/debug/highlights.py
index c6e544254..4e977b06a 100644
--- a/browser_use/dom/debug/highlights.py
+++ b/browser_use/dom/debug/highlights.py
@@ -18,10 +18,6 @@ def convert_dom_selector_map_to_highlight_format(selector_map: DOMSelectorMap) -
# Use absolute position which includes iframe coordinate translations
rect = node.absolute_position
bbox = {'x': rect.x, 'y': rect.y, 'width': rect.width, 'height': rect.height}
- elif node.snapshot_node and node.snapshot_node.bounds:
- # Fallback to local bounds if absolute position is not available
- rect = node.snapshot_node.bounds
- bbox = {'x': rect.x, 'y': rect.y, 'width': rect.width, 'height': rect.height}
# Only include elements with valid bounding boxes (following working implementation)
if bbox and bbox.get('width', 0) > 0 and bbox.get('height', 0) > 0:
@@ -114,33 +110,17 @@ async def inject_highlighting_script(dom_service: DomService, interactive_elemen
// High enough for most content but not maximum to avoid blocking critical popups/modals
const HIGHLIGHT_Z_INDEX = 999999; // High but reasonable z-index
- // Create container for all highlights - use absolute positioning to cover entire document
+ // Create container for all highlights - use fixed positioning without scroll calculations
const container = document.createElement('div');
container.id = 'browser-use-debug-highlights';
container.setAttribute('data-browser-use-highlight', 'container');
- // Get document dimensions to ensure container covers entire scrollable area
- const docHeight = Math.max(
- document.body.scrollHeight,
- document.body.offsetHeight,
- document.documentElement.clientHeight,
- document.documentElement.scrollHeight,
- document.documentElement.offsetHeight
- );
- const docWidth = Math.max(
- document.body.scrollWidth,
- document.body.offsetWidth,
- document.documentElement.clientWidth,
- document.documentElement.scrollWidth,
- document.documentElement.offsetWidth
- );
-
container.style.cssText = `
- position: absolute;
+ position: fixed;
top: 0;
left: 0;
- width: ${{docWidth}}px;
- height: ${{docHeight}}px;
+ width: 100vw;
+ height: 100vh;
pointer-events: none;
z-index: ${{HIGHLIGHT_Z_INDEX}};
overflow: visible;
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 83f6304d9..e1783ff5e 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -267,7 +267,7 @@ class DomService:
snapshot_lookup = build_snapshot_lookup(snapshot, device_pixel_ratio)
def _construct_enhanced_node(
- node: Node, html_frames: list[EnhancedDOMTreeNode] | None = None, total_iframe_offset: DOMRect | None = None
+ node: Node, html_frames: list[EnhancedDOMTreeNode] | None, total_frame_offset: DOMRect | None
) -> EnhancedDOMTreeNode:
"""
Recursively construct enhanced DOM tree nodes.
@@ -281,8 +281,13 @@ class DomService:
if html_frames is None:
html_frames = []
- if total_iframe_offset is None:
- total_iframe_offset = DOMRect(x=0.0, y=0.0, width=0.0, height=0.0)
+ # to get rid of the pointer references
+ if total_frame_offset is None:
+ total_frame_offset = DOMRect(x=0.0, y=0.0, width=0.0, height=0.0)
+ else:
+ total_frame_offset = DOMRect(
+ total_frame_offset.x, total_frame_offset.y, total_frame_offset.width, total_frame_offset.height
+ )
# memoize the mf (I don't know if some nodes are duplicated)
if node['nodeId'] in enhanced_dom_tree_node_lookup:
@@ -313,8 +318,8 @@ class DomService:
absolute_position = None
if snapshot_data and snapshot_data.bounds:
absolute_position = DOMRect(
- x=snapshot_data.bounds.x + total_iframe_offset.x,
- y=snapshot_data.bounds.y + total_iframe_offset.y,
+ x=snapshot_data.bounds.x + total_frame_offset.x,
+ y=snapshot_data.bounds.y + total_frame_offset.y,
width=snapshot_data.bounds.width,
height=snapshot_data.bounds.height,
)
@@ -351,20 +356,30 @@ class DomService:
if node['nodeType'] == NodeType.ELEMENT_NODE.value and node['nodeName'] == 'HTML' and node.get('frameId') is not None:
updated_html_frames.append(dom_tree_node)
+ # and adjust the total frame offset by scroll
+ if snapshot_data and snapshot_data.scrollRects:
+ total_frame_offset.x -= snapshot_data.scrollRects.x
+ total_frame_offset.y -= snapshot_data.scrollRects.y
+
# Calculate new iframe offset for content documents, accounting for iframe scroll
if node['nodeName'].upper() == 'IFRAME' and snapshot_data and snapshot_data.bounds:
if snapshot_data.bounds:
updated_html_frames.append(dom_tree_node)
+ total_frame_offset.x += snapshot_data.bounds.x
+ total_frame_offset.y += snapshot_data.bounds.y
+
if 'contentDocument' in node and node['contentDocument']:
- dom_tree_node.content_document = _construct_enhanced_node(node['contentDocument'], updated_html_frames)
+ dom_tree_node.content_document = _construct_enhanced_node(
+ node['contentDocument'], updated_html_frames, total_frame_offset
+ )
dom_tree_node.content_document.parent_node = dom_tree_node
# forcefully set the parent node to the content document node (helps traverse the tree)
if 'shadowRoots' in node and node['shadowRoots']:
dom_tree_node.shadow_roots = []
for shadow_root in node['shadowRoots']:
- shadow_root_node = _construct_enhanced_node(shadow_root, updated_html_frames)
+ shadow_root_node = _construct_enhanced_node(shadow_root, updated_html_frames, total_frame_offset)
# forcefully set the parent node to the shadow root node (helps traverse the tree)
shadow_root_node.parent_node = dom_tree_node
dom_tree_node.shadow_roots.append(shadow_root_node)
@@ -372,14 +387,14 @@ class DomService:
if 'children' in node and node['children']:
dom_tree_node.children_nodes = []
for child in node['children']:
- dom_tree_node.children_nodes.append(_construct_enhanced_node(child, updated_html_frames))
+ dom_tree_node.children_nodes.append(_construct_enhanced_node(child, updated_html_frames, total_frame_offset))
# Set visibility using the collected HTML frames
dom_tree_node.is_visible = self.is_element_visible_according_to_all_parents(dom_tree_node, updated_html_frames)
return dom_tree_node
- enhanced_dom_tree_node = _construct_enhanced_node(dom_tree['root'])
+ enhanced_dom_tree_node = _construct_enhanced_node(dom_tree['root'], None, None)
return enhanced_dom_tree_node