Skip to content

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

reverted multi-act back to branch_path_hashes

Gregor Žuniča29d4ac8bdea
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index df8f0d544..becaeca4a 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1492,7 +1492,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
and self.browser_session._cached_browser_state_summary.dom_state is not None
):
cached_selector_map = self.browser_session._cached_browser_state_summary.dom_state.selector_map
- cached_element_hashes = {hash(e) for e in cached_selector_map.values()}
+ cached_element_hashes = {e.parent_branch_hash() for e in cached_selector_map.values()}
else:
cached_selector_map = {}
cached_element_hashes = set()
@@ -1521,10 +1521,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):
new_selector_map = new_browser_state_summary.dom_state.selector_map
# Detect index change after previous action
- orig_target = cached_selector_map.get(action.get_index()) # type: ignore
- orig_target_hash = hash(orig_target) if orig_target else None
+ orig_target = cached_selector_map.get(action.get_index())
+ orig_target_hash = orig_target.parent_branch_hash() if orig_target else None
+
new_target = new_selector_map.get(action.get_index()) # type: ignore
- new_target_hash = hash(new_target) if new_target else None
+ new_target_hash = new_target.parent_branch_hash() if new_target else None
if orig_target_hash != new_target_hash:
msg = f'Element index changed after action {i} / {len(actions)}, because page changed.'
@@ -1539,7 +1540,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
break
# Check for new elements that appeared
- new_element_hashes = {hash(e) for e in new_selector_map.values()}
+ new_element_hashes = {e.parent_branch_hash() for e in new_selector_map.values()}
if check_for_new_elements and not new_element_hashes.issubset(cached_element_hashes):
# next action requires index but there are new elements on the page
msg = f'Something new appeared after action {i} / {len(actions)}, following actions are NOT executed and should be retried.'
diff --git a/browser_use/dom/playground/multi_act.py b/browser_use/dom/playground/multi_act.py
new file mode 100644
index 000000000..67c7ab839
--- /dev/null
+++ b/browser_use/dom/playground/multi_act.py
@@ -0,0 +1,32 @@
+from browser_use import Agent
+from browser_use.browser import BrowserProfile, BrowserSession
+from browser_use.browser.types import ViewportSize
+from browser_use.llm import ChatAzureOpenAI
+
+# Initialize the Azure OpenAI client
+llm = ChatAzureOpenAI(
+ model='gpt-4.1-mini',
+)
+
+
+TASK = """
+Go to https://browser-use.github.io/stress-tests/challenges/react-native-web-form.html and complete the React Native Web form by filling in all required fields and submitting.
+"""
+
+
+async def main():
+ browser = BrowserSession(
+ browser_profile=BrowserProfile(
+ window_size=ViewportSize(width=1100, height=1000),
+ )
+ )
+
+ agent = Agent(task=TASK, llm=llm)
+
+ await agent.run()
+
+
+if __name__ == '__main__':
+ import asyncio
+
+ asyncio.run(main())
diff --git a/browser_use/dom/views.py b/browser_use/dom/views.py
index 2e6e778ba..82c26fab8 100644
--- a/browser_use/dom/views.py
+++ b/browser_use/dom/views.py
@@ -409,6 +409,16 @@ class EnhancedDOMTreeNode:
# Convert to int for __hash__ return type - use first 16 chars and convert from hex to int
return int(element_hash[:16], 16)
+ def parent_branch_hash(self) -> int:
+ """
+ Hash the element based on its parent branch path and attributes.
+ """
+ parent_branch_path = self._get_parent_branch_path()
+ parent_branch_path_string = '/'.join(parent_branch_path)
+ element_hash = hashlib.sha256(parent_branch_path_string.encode()).hexdigest()
+
+ return int(element_hash[:16], 16)
+
def _get_parent_branch_path(self) -> list[str]:
"""Get the parent branch path as a list of tag names from root to current element."""
parents: list['EnhancedDOMTreeNode'] = []