Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

use go_to_url action to preload urls in tasks instead of force-navigating

Nick Sweeting35e122def173
diff --git a/browser_use/agent/cloud_events.py b/browser_use/agent/cloud_events.py
index 959fd4f36..127277fc8 100644
--- a/browser_use/agent/cloud_events.py
+++ b/browser_use/agent/cloud_events.py
@@ -157,12 +157,14 @@ class CreateAgentStepEvent(BaseEvent):
if browser_state_summary.screenshot:
screenshot_url = f'data:image/png;base64,{browser_state_summary.screenshot}'
import logging
+
logger = logging.getLogger(__name__)
logger.debug(f'πŸ“Έ Including screenshot in CreateAgentStepEvent, length: {len(browser_state_summary.screenshot)}')
else:
import logging
+
logger = logging.getLogger(__name__)
- logger.debug(f'πŸ“Έ No screenshot in browser_state_summary for CreateAgentStepEvent')
+ logger.debug('πŸ“Έ No screenshot in browser_state_summary for CreateAgentStepEvent')
return cls(
user_id='', # To be filled by cloud handler
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index 946bab866..ad63229d8 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -182,7 +182,7 @@ class AgentMessagePrompt:
recent_events_text = ''
if self.include_recent_events and self.browser_state.recent_events:
recent_events_text = f'Recent browser events: {self.browser_state.recent_events}\n'
-
+
browser_state = f"""{current_tab_text}
Available tabs:
{tabs_text}
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 27a2d084c..ffb295ec9 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -23,7 +23,6 @@ from browser_use.agent.cloud_events import (
UpdateAgentTaskEvent,
)
from browser_use.agent.message_manager.utils import save_conversation
-from browser_use.browser.events import BrowserStateRequestEvent
from browser_use.llm.base import BaseChatModel
from browser_use.llm.messages import BaseMessage, UserMessage
from browser_use.tokens.service import TokenCost
@@ -1279,24 +1278,45 @@ class Agent(Generic[Context, AgentStructuredOutput]):
# Start browser session and attach watchdogs
assert self.browser_session is not None, 'Browser session must be initialized before starting'
self.logger.debug('🌐 Starting browser session...')
- from browser_use.browser.events import BrowserStartEvent, NavigateToUrlEvent
+ from browser_use.browser.events import BrowserStartEvent
event = self.browser_session.event_bus.dispatch(BrowserStartEvent())
await event
self.logger.debug('πŸ”§ Browser session started with watchdogs attached')
- # Check if task contains a URL and navigate to it immediately (only if preload is enabled)
+ # Check if task contains a URL and add it as an initial action (only if preload is enabled)
if self.preload:
initial_url = self._extract_url_from_task(self.task)
if initial_url:
- self.logger.info(f'πŸ”— Found URL in task: {initial_url}, navigating immediately...')
- self.browser_session.event_bus.dispatch(NavigateToUrlEvent(url=initial_url))
-
- # warms up the cache and element highlighting is more useful as a better loading animation than a DVD screensaver
- self.browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_dom=True, include_screenshot=True))
+ self.logger.info(f'πŸ”— Found URL in task: {initial_url}, adding as initial action...')
+
+ # Create a go_to_url action for the initial URL
+ go_to_url_action = {
+ 'go_to_url': {
+ 'url': initial_url,
+ 'new_tab': False, # Navigate in current tab
+ }
+ }
+
+ # Add to initial_actions or create new list if none exist
+ if self.initial_actions:
+ # Convert back to dict format, prepend URL navigation, then convert back
+ initial_actions_dicts = []
+ for action in self.initial_actions:
+ action_data = action.model_dump(exclude_unset=True)
+ initial_actions_dicts.append(action_data)
+
+ # Prepend the go_to_url action
+ initial_actions_dicts = [go_to_url_action] + initial_actions_dicts
+
+ # Convert back to ActionModel instances
+ self.initial_actions = self._convert_initial_actions(initial_actions_dicts)
+ else:
+ # Create new initial_actions with just the go_to_url
+ self.initial_actions = self._convert_initial_actions([go_to_url_action])
- self.logger.debug(f'βœ… Navigated to {initial_url}')
+ self.logger.debug(f'βœ… Added navigation to {initial_url} as initial action')
# Execute initial actions if provided
if self.initial_actions: