Skip to content

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

Refactor cross-origin iframe click test for improved clarity and functionality

Magnus Müllerf3c707050691
diff --git a/browser_use/dom/serializer/serializer.py b/browser_use/dom/serializer/serializer.py
index 77d730489..a69c7701d 100644
--- a/browser_use/dom/serializer/serializer.py
+++ b/browser_use/dom/serializer/serializer.py
@@ -440,10 +440,6 @@ class DOMTreeSerializer:
self._clickable_cache[node.node_id] = result
- # Debug for cross-origin iframe elements - show result of first check
- if node.target_id and node.attributes and 'iana.org' in node.attributes.get('href', ''):
- target_short = node.target_id[-4:] if node.target_id else 'None'
-
return self._clickable_cache[node.node_id]
def _create_simplified_tree(self, node: EnhancedDOMTreeNode, depth: int = 0) -> SimplifiedNode | None:
diff --git a/tests/ci/browser/test_cross_origin_click.py b/tests/ci/browser/test_cross_origin_click.py
index afda46bb6..90515cee0 100644
--- a/tests/ci/browser/test_cross_origin_click.py
+++ b/tests/ci/browser/test_cross_origin_click.py
@@ -31,95 +31,109 @@ class TestCrossOriginIframeClick:
"""Test clicking elements inside cross-origin iframes."""
async def test_click_element_in_cross_origin_iframe(self, httpserver, browser_session: BrowserSession):
- """Verify that elements inside cross-origin iframes can be clicked."""
+ """Verify that elements inside iframes in different CDP targets can be clicked."""
- # Create main page with cross-origin iframe pointing to example.com
- # example.com has a "Learn more" link that we'll try to click
+ # Create iframe content with clickable elements
+ iframe_html = """
+ <!DOCTYPE html>
+ <html>
+ <head><title>Iframe Page</title></head>
+ <body>
+ <h1>Iframe Content</h1>
+ <a href="https://test-domain.example/page" id="iframe-link">Test Link</a>
+ <button id="iframe-button">Iframe Button</button>
+ </body>
+ </html>
+ """
+
+ # Create main page with iframe pointing to our test server
main_html = """
<!DOCTYPE html>
<html>
- <head><title>Cross-Origin Test</title></head>
+ <head><title>Multi-Target Test</title></head>
<body>
<h1>Main Page</h1>
<button id="main-button">Main Button</button>
- <iframe id="cross-origin" src="https://example.com" style="width: 800px; height: 600px;"></iframe>
+ <iframe id="test-iframe" src="/iframe-content" style="width: 800px; height: 600px;"></iframe>
</body>
</html>
"""
- # Serve the main page
- httpserver.expect_request('/cross-origin-test').respond_with_data(main_html, content_type='text/html')
- url = httpserver.url_for('/cross-origin-test')
+ # Serve both pages
+ httpserver.expect_request('/multi-target-test').respond_with_data(main_html, content_type='text/html')
+ httpserver.expect_request('/iframe-content').respond_with_data(iframe_html, content_type='text/html')
+ url = httpserver.url_for('/multi-target-test')
# Navigate to the page
await browser_session.navigate_to(url)
- # Wait longer for cross-origin iframe to load (network can be slow in CI)
- await asyncio.sleep(5)
+ # Wait for iframe to load
+ await asyncio.sleep(2)
# Get DOM state with cross-origin iframe extraction enabled
- dom_service = DomService(browser_session, cross_origin_iframes=True)
- state, _, _ = await dom_service.get_serialized_dom_tree()
-
- # IMPORTANT: Update cached selector map so get_element_by_index() can find elements
- # This is normally done by DOMWatchdog, but we're calling DomService directly in the test
- browser_session.update_cached_selector_map(state.selector_map)
+ # Use browser_session.get_browser_state_summary() instead of directly creating DomService
+ # This goes through the proper event bus and watchdog system
+ browser_state = await browser_session.get_browser_state_summary(
+ include_screenshot=False,
+ include_recent_events=False,
+ )
+ assert browser_state.dom_state is not None
+ state = browser_state.dom_state
print(f'\n📊 Found {len(state.selector_map)} total elements')
# Find elements from different targets
targets_found = set()
main_page_elements = []
- cross_origin_elements = []
+ iframe_elements = []
for idx, element in state.selector_map.items():
target_id = element.target_id
targets_found.add(target_id)
- # Check if element is from cross-origin iframe (example.com)
- # Cross-origin elements will have a different target_id than main page
- if element.attributes and 'iana.org' in element.attributes.get('href', ''):
- cross_origin_elements.append((idx, element))
- print(f' ✅ Found cross-origin element: [{idx}] {element.tag_name} href={element.attributes.get("href")}')
- elif element.attributes and element.attributes.get('id') == 'main-button':
- main_page_elements.append((idx, element))
+ # Check if element is from iframe (identified by id attributes we set)
+ # Iframe elements will have a different target_id when cross_origin_iframes=True
+ if element.attributes:
+ element_id = element.attributes.get('id', '')
+ if element_id in ('iframe-link', 'iframe-button'):
+ iframe_elements.append((idx, element))
+ print(f' ✅ Found iframe element: [{idx}] {element.tag_name} id={element_id}')
+ elif element_id == 'main-button':
+ main_page_elements.append((idx, element))
# Verify we found elements from at least 2 different targets
print(f'\n🎯 Found elements from {len(targets_found)} different CDP targets')
- # Check if cross-origin iframe loaded
- if len(targets_found) < 2 or len(cross_origin_elements) == 0:
- # Cross-origin iframe didn't load (network issue in CI or timing)
- print('⚠️ Warning: Cross-origin iframe did not load (network/timing issue)')
- print(' This is expected in some CI environments with restricted network access')
- pytest.skip('Cross-origin iframe did not load - skipping cross-origin click test')
+ # Check if iframe elements were found
+ if len(iframe_elements) == 0:
+ pytest.fail('Expected to find at least one element from iframe, but found none')
- # Verify we found at least one element from the cross-origin iframe
- assert len(cross_origin_elements) > 0, 'Expected to find at least one element from cross-origin iframe (example.com)'
+ # Verify we found at least one element from the iframe
+ assert len(iframe_elements) > 0, 'Expected to find at least one element from iframe'
- # Try clicking the cross-origin element
- print('\n🖱️ Testing Click on Cross-Origin Iframe Element:')
+ # Try clicking the iframe element
+ print('\n🖱️ Testing Click on Iframe Element:')
tools = Tools()
- link_idx, link_element = cross_origin_elements[0]
- print(f' Attempting to click element [{link_idx}] from cross-origin iframe...')
+ link_idx, link_element = iframe_elements[0]
+ print(f' Attempting to click element [{link_idx}] from iframe...')
try:
result = await tools.click(index=link_idx, browser_session=browser_session)
# Check for errors
if result.error:
- pytest.fail(f'Click on cross-origin element [{link_idx}] failed with error: {result.error}')
+ pytest.fail(f'Click on iframe element [{link_idx}] failed with error: {result.error}')
if result.extracted_content and (
'not available' in result.extracted_content.lower() or 'failed' in result.extracted_content.lower()
):
- pytest.fail(f'Click on cross-origin element [{link_idx}] failed: {result.extracted_content}')
+ pytest.fail(f'Click on iframe element [{link_idx}] failed: {result.extracted_content}')
- print(f' ✅ Click succeeded on cross-origin element [{link_idx}]!')
- print(' 🎉 Cross-origin iframe element clicking works!')
+ print(f' ✅ Click succeeded on iframe element [{link_idx}]!')
+ print(' 🎉 Iframe element clicking works!')
except Exception as e:
- pytest.fail(f'Exception while clicking cross-origin element [{link_idx}]: {e}')
+ pytest.fail(f'Exception while clicking iframe element [{link_idx}]: {e}')
- print('\n✅ Test passed: Cross-origin iframe elements can be clicked')
+ print('\n✅ Test passed: Iframe elements can be clicked')