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
Fix rerun with inital actions
Magnus Müller064c5ba0168b
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 0b0aa2fb5..bbc0ef22c 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -1772,26 +1772,26 @@ class Agent(Generic[Context, AgentStructuredOutput]):Returns:List of action results"""- # Initialize browser session first (same as run function)- await self.browser_session.start()+ # Skip cloud sync session events for rerunning (we're replaying, not starting new)+ self.state.session_initialized = True- # Execute initial actions if provided- if self.initial_actions:- result = await self.multi_act(self.initial_actions)- self.state.last_result = result+ # Initialize browser session+ await self.browser_session.start()results = []for i, history_item in enumerate(history.history):goal = history_item.model_output.current_state.next_goal if history_item.model_output else ''- self.logger.info(f'Replaying step {i + 1}/{len(history.history)}: goal: {goal}')+ step_num = history_item.metadata.step_number if history_item.metadata else i+ step_name = 'Initial actions' if step_num == 0 else f'Step {step_num}'+ self.logger.info(f'Replaying {step_name} ({i + 1}/{len(history.history)}): {goal}')if (not history_item.model_outputor not history_item.model_output.actionor history_item.model_output.action == [None]):- self.logger.warning(f'Step {i + 1}: No action to replay, skipping')+ self.logger.warning(f'{step_name}: No action to replay, skipping')results.append(ActionResult(error='No action to replay'))continue@@ -1805,13 +1805,13 @@ class Agent(Generic[Context, AgentStructuredOutput]):except Exception as e:retry_count += 1if retry_count == max_retries:- error_msg = f'Step {i + 1} failed after {max_retries} attempts: {str(e)}'+ error_msg = f'{step_name} failed after {max_retries} attempts: {str(e)}'self.logger.error(error_msg)if not skip_failures:results.append(ActionResult(error=error_msg))raise RuntimeError(error_msg)else:- self.logger.warning(f'Step {i + 1} failed (attempt {retry_count}/{max_retries}), retrying...')+ self.logger.warning(f'{step_name} failed (attempt {retry_count}/{max_retries}), retrying...')await asyncio.sleep(delay_between_actions)return results@@ -1825,6 +1825,40 @@ class Agent(Generic[Context, AgentStructuredOutput]):if result and self.initial_url and result[0].long_term_memory:result[0].long_term_memory = f'Found initial url and automatically loaded it. {result[0].long_term_memory}'self.state.last_result = result++ # Save initial actions to history as step 0 for rerun capability+ # Skip browser state capture for initial actions (usually just URL navigation)+ model_output = self.AgentOutput(+ evaluation_previous_goal='Starting agent with initial actions',+ memory='',+ next_goal='Execute initial navigation or setup actions',+ action=self.initial_actions,+ )++ metadata = StepMetadata(+ step_number=0,+ step_start_time=time.time(),+ step_end_time=time.time(),+ )++ # Create minimal browser state history for initial actions+ state_history = BrowserStateHistory(+ url=self.initial_url or '',+ title='Initial Actions',+ tabs=[],+ interacted_element=[None] * len(self.initial_actions), # No DOM elements needed+ screenshot_path=None,+ )++ history_item = AgentHistory(+ model_output=model_output,+ result=result,+ state=state_history,+ metadata=metadata,+ )++ self.history.add_item(history_item)+ self.logger.debug('📝 Saved initial actions to history as step 0')self.logger.debug('Initial actions completed')async def _execute_history_step(self, history_item: AgentHistory, delay: float) -> list[ActionResult]:diff --git a/browser_use/dom/views.py b/browser_use/dom/views.pyindex 06b1e8b32..ea97c70b3 100644--- a/browser_use/dom/views.py+++ b/browser_use/dom/views.py@@ -146,6 +146,17 @@ class DOMRect:width: floatheight: float+ def to_dict(self) -> dict[str, Any]:+ return {+ 'x': self.x,+ 'y': self.y,+ 'width': self.width,+ 'height': self.height,+ }++ def __json__(self) -> dict:+ return self.to_dict()+@dataclass(slots=True)class EnhancedAXProperty:@@ -758,11 +769,16 @@ class DOMInteractedElement:def to_dict(self) -> dict[str, Any]:return {+ 'node_id': self.node_id,+ 'backend_node_id': self.backend_node_id,+ 'frame_id': self.frame_id,'node_type': self.node_type.value,'node_value': self.node_value,'node_name': self.node_name,'attributes': self.attributes,'x_path': self.x_path,+ 'element_hash': self.element_hash,+ 'bounds': self.bounds.to_dict(),}@classmethod