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
Detect index change caused by page change in `multi_act`
Alin Jiangf7f193a61d0a
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex bd6615de5..0a78bd17d 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -659,7 +659,20 @@ class Agent(Generic[Context]):for i, action in enumerate(actions):if action.get_index() is not None and i != 0:new_state = await self.browser_context.get_state()- new_path_hashes = set(e.hash.branch_path_hash for e in new_state.selector_map.values())+ new_selector_map = new_state.selector_map++ # Detect index change after previous action+ orig_target = cached_selector_map.get(action.get_index()) # type: ignore+ orig_target_hash = orig_target.hash.branch_path_hash if orig_target else None+ new_target = new_selector_map.get(action.get_index()) # type: ignore+ new_target_hash = new_target.hash.branch_path_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.'+ logger.info(msg)+ results.append(ActionResult(extracted_content=msg, include_in_memory=True))+ break++ new_path_hashes = set(e.hash.branch_path_hash for e in new_selector_map.values())if check_for_new_elements and not new_path_hashes.issubset(cached_path_hashes):# next action requires index but there are new elements on the pagemsg = f'Something new appeared after action {i} / {len(actions)}'