-
Notifications
You must be signed in to change notification settings - Fork 1k
docs(interaction-skills): flesh out cross-origin-iframes.md #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shashwatgokhe
wants to merge
1
commit into
browser-use:main
Choose a base branch
from
shashwatgokhe:docs/cross-origin-iframes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,69 @@ | ||
| # Cross-Origin Iframes | ||
|
|
||
| Focus on `iframe_target(...)`, target attachment, and when compositor-level coordinate clicks are lower-friction than cross-target DOM work. | ||
| A cross-origin iframe runs in a different renderer process than its parent. `js("...")` against the top-level page cannot reach into it (`document.querySelector` returns `null`, `contentDocument` throws SecurityError). Two ways through. | ||
|
|
||
| ## Default: coordinate clicks pass through | ||
|
|
||
| `click_at_xy(x, y)` dispatches `Input.dispatchMouseEvent` at the browser process. Hit-testing happens on the compositor, so the click lands on whatever pixel is on top — the iframe's renderer receives it natively. No DOM work needed. | ||
|
|
||
| ```python | ||
| capture_screenshot("/tmp/shot.png") | ||
| # read the pixel of the button inside the iframe | ||
| click_at_xy(420, 580) | ||
| capture_screenshot("/tmp/shot.png") # verify | ||
| ``` | ||
|
|
||
| This is the right tool for buttons, links, tabs, menu items, and most UI inside web IDEs, embedded dashboards, payment forms, and SSO flows. | ||
|
|
||
| ## When you need DOM access: `iframe_target()` + `js(target_id=...)` | ||
|
|
||
| For reading text out of the iframe, querying the structure, or focusing a specific element by selector: | ||
|
|
||
| ```python | ||
| tid = iframe_target("vscode-web") # substring match on the iframe's URL | ||
| title = js("document.title", target_id=tid) | ||
| # focus a specific input inside the iframe | ||
| js("document.querySelector('.monaco-editor textarea').focus()", target_id=tid) | ||
| ``` | ||
|
|
||
| `iframe_target()` returns the first `iframe`-type CDP target whose URL contains the substring, or `None`. Pick a substring that's unique to that iframe — a hostname or path fragment, not just `https://`. | ||
|
|
||
| If the page has multiple cross-origin iframes from the same origin, walk `cdp("Target.getTargets")["targetInfos"]` yourself and pick by URL or by parent. | ||
|
|
||
| ## Typing into a cross-origin iframe | ||
|
|
||
| `type_text()` and `press_key()` dispatch through `Input.insertText` / `Input.dispatchKeyEvent` at the browser process. They go to whichever element currently has focus inside Chrome — the iframe boundary is not a barrier, but **focus must already be on an element inside the iframe**. | ||
|
|
||
| Two ways to give focus to the right element: | ||
|
|
||
| ```python | ||
| # A) coordinate click into the iframe (preferred — same as a real user) | ||
| click_at_xy(420, 300) | ||
| type_text("hello world") | ||
|
|
||
| # B) focus by selector via the iframe's own target | ||
| tid = iframe_target("vscode-web") | ||
| js("document.querySelector('.monaco-editor textarea').focus()", target_id=tid) | ||
| type_text("hello world") | ||
| ``` | ||
|
|
||
| If keystrokes seem to land in the wrong place, take a screenshot — focus usually moved (a modal popped, the iframe blurred, an inspector panel opened). Re-click into the iframe and continue. | ||
|
|
||
| ## Pitfalls | ||
|
|
||
| - `js("document.querySelectorAll('iframe')")` from the parent only sees the iframe **element**, not the document inside it. `querySelector(...).contentDocument` throws on cross-origin. Use `iframe_target()` instead. | ||
| - `iframe_target()` can return `None` if the iframe is still loading. Either `wait_for_load()` first or poll: `while iframe_target("...") is None: wait(0.5)`. | ||
| - A page can have nested cross-origin iframes (iframe-in-iframe). Each is its own CDP target — `iframe_target()` matches by URL substring, so use a path fragment specific to the inner one. | ||
| - After a navigation inside the iframe, the target ID changes. Re-fetch with `iframe_target()` rather than caching it across navigations. | ||
| - Some sandboxed iframes (`sandbox="allow-scripts"` without `allow-same-origin`) refuse focus events from `Input.dispatchKeyEvent` if focus was set via JS but never via user gesture. Click first, then type — don't try to skip the click. | ||
| - Drag, drop, and file-upload in cross-origin iframes need the iframe's own target. See `uploads.md`. | ||
|
|
||
| ## Quick decision | ||
|
|
||
| | Want to | Use | | ||
| |---|---| | ||
| | Click a button / tab / menu item | `click_at_xy(x, y)` (no iframe code) | | ||
| | Type into a focused input | `click_at_xy(...)` then `type_text(...)` | | ||
| | Read text or DOM structure | `js("...", target_id=iframe_target("..."))` | | ||
| | Focus by selector (no visible target) | `js("...focus()", target_id=...)` then `type_text(...)` | | ||
| | File upload, drag, complex DOM | iframe target work — see `uploads.md` / `drag-and-drop.md` | | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The recommended polling loop can run forever if the iframe never appears; add a timeout/deadline or use a bounded wait helper.
Prompt for AI agents