Skip to content

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

Fix for nested iframes

Sergey Petrov1267c4d275d7
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 3e506ac55..d862cdfc5 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -677,7 +677,7 @@ class BrowserContext:
# Handle numeric indices
if idx.isdigit():
index = int(idx) - 1
- base_part += f':nth-of-type({index+1})'
+ base_part += f':nth-of-type({index + 1})'
# Handle last() function
elif idx == 'last()':
base_part += ':last-of-type'
@@ -803,13 +803,13 @@ class BrowserContext:
parent = current.parent
parents.append(parent)
current = parent
- if parent.tag_name == 'iframe':
- break
- # There can be only one iframe parent (by design of the loop above)
- iframe_parent = [item for item in parents if item.tag_name == 'iframe']
- if iframe_parent:
- parent = iframe_parent[0]
+ # Reverse the parents list to process from top to bottom
+ parents.reverse()
+
+ # Process all iframe parents in sequence
+ iframes = [item for item in parents if item.tag_name == 'iframe']
+ for parent in iframes:
css_selector = self._enhanced_css_selector_for_element(parent)
current_frame = current_frame.frame_locator(css_selector)