browser-use: browser-use does not highlight elements in iframe
Browser Use Version
0.1.45
LLM Model
Other (specify in description), gpt-4o
Screenshots, Description, and Task Prompt Given to Agent
same problem with o4-mini
I currently have to add the following hack to get the agent to click buttons in an iframe
@controller.action("Click iframe button")
async def click_iframe_button(button_text: str, browser: Browser):
page = await browser.get_current_page()
for frame in page.frames:
# Look for an element (e.g. button or link) containing the desired text
element = await frame.query_selector(f"text=\"{button_text}\"")
if element:
await element.click()
return ActionResult(extracted_content=f"Clicked '{button_text}' in iframe")
return ActionResult(extracted_content=f"No '{button_text}' button found in iframes")
without it, the agent tries to click the play button but cannot as it is not highlighted. my hack requires manually finding the CSS selector for each text-less button.
HTML around where it’s failing
content slightly modified to not be identifiable. but this html isnt helpful to you to debug as the main issue is that this html button is in an iframe.
<svg id="playBtnS" class="fkldsajljsl" width="14" height="19" fill="#fff" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" ></path></svg>
Operating System
macos sonoma 14.6.1
Python Code Sample
from browser_use import Agent, Browser, BrowserConfig, Controller, ActionResult
from langchain_openai import ChatOpenAI
import asyncio
controller = Controller()
@controller.action("Click iframe button")
async def click_iframe_button(button_text: str, browser: Browser):
page = await browser.get_current_page()
for frame in page.frames:
# Look for an element (e.g. button or link) containing the desired text
element = await frame.query_selector(f"text=\"{button_text}\"")
if element:
await element.click()
return ActionResult(extracted_content=f"Clicked '{button_text}' in iframe")
return ActionResult(extracted_content=f"No '{button_text}' button found in iframes")
@controller.action("Click iframe selector")
async def click_iframe_selector(css_selector: str, browser: Browser):
"""
Click first element matching `css_selector` in any frame.
Also tries clicking the closest <button>/<div> if the element itself
(e.g. an <svg>) is not directly clickable.
"""
page = await browser.get_current_page()
for frame in page.frames:
el = await frame.query_selector(css_selector)
if el and await el.is_visible():
try:
await el.click()
except Exception:
# e.g. SVG not receiving pointer events → click nearest button
parent = await el.evaluate_handle(
'(e)=>e.closest("button,div")')
if parent:
await parent.as_element().click()
else:
raise
return ActionResult(
extracted_content=f"Clicked selector '{css_selector}' in iframe"
)
return ActionResult(
extracted_content=f"Selector '{css_selector}' not found in any iframe"
)
# Configure the browser to connect to your Chrome instance
browser = Browser(
config=BrowserConfig(
browser_binary_path='/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
disable_security=True,
)
)
extend_system_message = "If an element is not found in the main page DOM, use the supplied 'Click iframe buton` action with the text in the button. only if the button does not have text, call 'Click iframe selector' with a CSS selector. the CSS selector for the play button is '#playBtnS'. For other text-less buttons suspected to be in iframe, the CSS is not known: you may have to guess."
agent = Agent(
task=("Open .... and work through the tutorial. "
"If the page shows a video, click play-like buttons until the video starts playing. after that the page may gray out the progress buttons until the video finishes."
"Repeat until you hit the tutorial complete screen."),
llm=ChatOpenAI(model="o4-mini", disabled_params={"parallel_tool_calls": None}),
browser=browser,
extend_system_message=extend_system_message,
controller=controller,
)
async def main():
await agent.run()
input('Press Enter to close the browser...')
await browser.close()
if __name__ == '__main__':
asyncio.run(main())
Full DEBUG Log Output
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 21 (9 by maintainers)
That didnt help. The exact code you suggested above ran into cloudflare issues. GPT o3 suggested connecting to a live chromium to avoid that, so I tried the following. It still does not highlight anything in the iframes
I can do a private zoom call to debug this live, but I cannot publicly reveal the website.