Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
lint and test fixes
Nick Sweetingcdf941b0e895
diff --git a/browser_use/browser/downloads_watchdog.py b/browser_use/browser/downloads_watchdog.pyindex 407874653..212816f29 100644--- a/browser_use/browser/downloads_watchdog.py+++ b/browser_use/browser/downloads_watchdog.py@@ -125,7 +125,8 @@ class DownloadsWatchdog(BaseWatchdog):logger.info(f'[DownloadsWatchdog] Handling download: {download.suggested_filename} from {download.url[:100]}...')# Debug: Check if download is already being handled elsewhere- logger.info(f'[DownloadsWatchdog] Download state - canceled: {download.failure()}, url: {download.url}')+ failure = await download.failure()+ logger.info(f'[DownloadsWatchdog] Download state - canceled: {failure}, url: {download.url}')logger.info(f'[DownloadsWatchdog] Active downloads count: {len(self._active_downloads)}')try:diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex cdc291675..1585f4f7f 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -491,7 +491,9 @@ class BrowserSession(BaseModel):new_tab_msg = 'New tab opened - switching to it'msg += f' - {new_tab_msg}'logger.info(f'🔗 {new_tab_msg}')- await self.switch_to_tab(-1)+ # Switch to the last tab (newly created tab)+ last_tab_index = len(self.pages) - 1+ await self.switch_to_tab(last_tab_index)except Exception as e:self.event_bus.dispatch(@@ -1541,13 +1543,34 @@ class BrowserSession(BaseModel):if new_tab:import sys+ # Track initial tab count before clicking+ initial_tab_count = len(self.pages)+modifier = 'Meta' if sys.platform == 'darwin' else 'Control'await locator.click(modifiers=[modifier])++ # Wait for new tab to be created (up to 5 seconds)+ try:+ await asyncio.wait_for(self._wait_for_new_tab(initial_tab_count), timeout=5.0)+ logger.debug(f'New tab opened, tab count: {len(self.pages)}')+ except TimeoutError:+ logger.debug('No new tab opened within 5 seconds')else:await locator.click()return download_path+ async def _wait_for_new_tab(self, initial_tab_count: int) -> None:+ """Wait for a new tab to be created."""+ # Wait for TabCreatedEvent instead of polling+ from browser_use.browser.events import TabCreatedEvent++ try:+ await self.event_bus.expect(TabCreatedEvent, timeout=5.0)+ except TimeoutError:+ # No new tab was created within timeout+ pass+async def _input_text_element_node(self, element: Any, text: str) -> None:"""Input text into an element node."""if not element:diff --git a/browser_use/browser/storage_state_watchdog.py b/browser_use/browser/storage_state_watchdog.pyindex 764b62b99..ce17439e4 100644--- a/browser_use/browser/storage_state_watchdog.py+++ b/browser_use/browser/storage_state_watchdog.py@@ -7,7 +7,7 @@ from pathlib import Pathfrom typing import Any, ClassVarfrom bubus import BaseEvent-from playwright.async_api import Cookie, Page+from playwright.async_api import Cookie, Page, StorageStateCookiefrom pydantic import Field, PrivateAttrfrom browser_use.browser.events import (@@ -43,7 +43,7 @@ class StorageStateWatchdog(BaseWatchdog):# Private state_monitoring_task: asyncio.Task | None = PrivateAttr(default=None)- _last_cookie_state: list[Cookie] = PrivateAttr(default_factory=list)+ _last_cookie_state: list[StorageStateCookie] = PrivateAttr(default_factory=list)_save_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)async def on_BrowserStartedEvent(self, event: BrowserStartedEvent) -> None:@@ -158,7 +158,9 @@ class StorageStateWatchdog(BaseWatchdog):return Falsetry:- current_cookies = await self.browser_session._browser_context.cookies()+ # Get current storage state cookies (not Cookie API objects)+ storage_state = await self.browser_session._browser_context.storage_state()+ current_cookies = storage_state.get('cookies', [])# Convert to comparable format, using .get() for optional fieldscurrent_cookie_set = {diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 8bb3ed1b9..ee1f0b845 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -190,7 +190,9 @@ class Controller(Generic[Context]):msg += f' - {new_tab_msg}'emoji = '🔗'logger.info(f'{emoji} {new_tab_msg}')- await browser_session.switch_to_tab(-1)+ # Switch to the last tab (newly created tab)+ last_tab_index = len(browser_session.tabs) - 1+ await browser_session.switch_to_tab(last_tab_index)logger.info(f'{emoji} {msg}')logger.debug(f'Element xpath: {element_node.xpath}')