Skip to content

feat: add close_tab() helper to close browser tabs via CDP Target.closeTarget#337

Open
stofancy wants to merge 1 commit into
browser-use:mainfrom
stofancy:feat/close-tab-helper
Open

feat: add close_tab() helper to close browser tabs via CDP Target.closeTarget#337
stofancy wants to merge 1 commit into
browser-use:mainfrom
stofancy:feat/close-tab-helper

Conversation

@stofancy
Copy link
Copy Markdown

@stofancy stofancy commented May 10, 2026

Adds close_tab(target=None) — the symmetric counterpart to new_tab() that was missing from the tab operations section.

Why

Each Chrome tab is a separate renderer process (~50-200MB). After an automation task opens several tabs, there was no convenient way to clean them up — you had to drop to raw cdp("Target.closeTarget", targetId=id). This adds a first-class helper alongside list_tabs / new_tab / switch_tab.

What

def close_tab(target=None):
    """Close a tab. If `target` is omitted, closes the currently attached tab.
    Accepts a raw targetId string or a dict from list_tabs()/current_tab()."""
    target_id = target.get("targetId") if isinstance(target, dict) else target
    if target_id is None:
        target_id = current_tab()["targetId"]
    cdp("Target.closeTarget", targetId=target_id)

Usage

# Close a specific tab
tid = new_tab("https://example.com")
# ... do work ...
close_tab(tid)

# Close current tab
close_tab()

# Bulk cleanup — keep current, close rest
tabs = list_tabs(include_chrome=False)
cur = current_tab()["targetId"]
for t in tabs:
    if t["targetId"] != cur:
        close_tab(t)

Placement

Placed right after new_tab() and before ensure_real_tab() in the # --- tabs --- section, following the same conventions for argument handling (accepts both dict and raw string).


Summary by cubic

Add close_tab() to close Chrome tabs via CDP, making it easy to clean up tabs and free memory. Complements new_tab()/switch_tab() for full tab lifecycle control.

  • New Features
    • close_tab(target=None) closes a specific tab or the current tab when omitted.
    • Accepts a targetId string or a tab dict from list_tabs()/current_tab().
    • Uses CDP Target.closeTarget under the hood.

Written for commit e0e7f0b. Summary will update on new commits.

…seTarget

Adds close_tab(target=None) alongside new_tab()/switch_tab() as the
missing symmetric tab operation. Accepts a targetId string, a tab dict
from list_tabs()/current_tab(), or no argument to close the currently
attached tab.

Each Chrome tab is a separate renderer process (~50-200MB); closing
unused tabs after an automation task prevents resource bloat.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant