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
add RefreshEvent
Nick Sweeting762b3d10a70b
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 74f9b08e0..33f72f654 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -14,6 +14,7 @@ from browser_use.browser.events import (ClickElementEvent,GoBackEvent,GoForwardEvent,+ RefreshEvent,ScrollEvent,ScrollToTextEvent,SendKeysEvent,@@ -603,6 +604,30 @@ class DefaultActionWatchdog(BaseWatchdog):))+ async def on_RefreshEvent(self, event: RefreshEvent) -> None:+ """Handle page refresh request with CDP."""+ try:+ # Get CDP client and session+ cdp_client = await self.browser_session.get_cdp_client()+ session_id = await self.browser_session.get_current_page_cdp_session_id()++ # Reload the page+ await cdp_client.send.Page.reload(session_id=session_id)++ # Wait for reload+ await asyncio.sleep(1.0)+ page = await self.browser_session.get_current_page()+ await self.browser_session._check_and_handle_navigation(page)++ logger.info('🔄 Page refreshed')+ except Exception as e:+ self.event_bus.dispatch(+ BrowserErrorEvent(+ error_type='RefreshFailed',+ message=str(e),+ )+ )+async def on_WaitEvent(self, event: WaitEvent) -> None:"""Handle wait request."""try:diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex 36f583e29..9f3e2d7ba 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -93,6 +93,12 @@ class GoForwardEvent(BaseEvent):pass+class RefreshEvent(BaseEvent):+ """Refresh/reload the current page."""++ pass++class WaitEvent(BaseEvent):"""Wait for a specified number of seconds."""diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 44269b5ee..822434288 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -27,6 +27,7 @@ from browser_use.browser.events import (GoBackEvent,GoForwardEvent,NavigateToUrlEvent,+ RefreshEvent,SaveStorageStateEvent,ScreenshotEvent,ScrollEvent,@@ -59,7 +60,7 @@ from browser_use.browser.views import (# Lazy imports for heavy DOM services to improve startup time# from browser_use.dom.clickable_element_processor.service import ClickableElementProcessor# from browser_use.dom.service import DomService-from browser_use.dom.views import DOMElementNode, SelectorMap+from browser_use.dom.views import EnhancedDOMTreeNode as DOMElementNode, DOMSelectorMap as SelectorMapfrom browser_use.utils import (is_new_tab_page,time_execution_async,@@ -2710,9 +2711,8 @@ class BrowserSession(BaseModel):async def refresh(self) -> None:"""Refresh the current page."""- page = await self.get_current_page()- if page:- await page.reload()+ event = self.event_bus.dispatch(RefreshEvent())+ await eventasync def take_screenshot(self, full_page: bool = False, clip: dict | None = None) -> str | None:"""Take a screenshot."""