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
improving the element click to fallback to js evaluate; use optional element type
jersobhf6960ad80e04
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.pyindex f11b6bd25..acfd5bbd7 100644--- a/browser_use/browser/context.py+++ b/browser_use/browser/context.py@@ -1165,14 +1165,17 @@ class BrowserContext:return None@time_execution_async('--get_locate_element_by_text')- async def get_locate_element_by_text(self, text: str, nth: Optional[int] = 0) -> Optional[ElementHandle]:+ async def get_locate_element_by_text(self, text: str, nth: Optional[int] = 0, element_type: Optional[str] = None) -> Optional[ElementHandle]:"""Locates an element on the page using the provided text.If `nth` is provided, it returns the nth matching element (0-based).+ If `element_type` is provided, filters by tag name (e.g., 'button', 'span')."""current_frame = await self.get_current_page()try:- elements = await current_frame.query_selector_all(f"text={text}")+ # handle also specific element type or use any type.+ selector = f"{element_type or '*'}:text(\"{text}\")"+ elements = await current_frame.query_selector_all(selector)# considering only visible elementselements = [el for el in elements if await el.is_visible()]diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 7c3f78b45..c8bf1368e 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -171,8 +171,17 @@ class Controller(Generic[Context]):try:element_node = await browser.get_locate_element_by_xpath(params.xpath)if element_node:- await element_node.click()- msg = f'🖱️ Clicked on element with xpath {params.xpath}'+ try:+ await element_node.scroll_into_view_if_needed()+ await element_node.click(timeout=1500, force=True)+ except Exception as e:+ try:+ # Handle with js evaluate if fails to click using playwright+ await element_node.evaluate('el => el.click()')+ except Exception as e:+ logger.warning(f"Element not clickable with text '{params.xpath}' - {e}")+ return ActionResult(error=str(e))+ msg = f'🖱️ Clicked on element with text "{params.xpath}"'return ActionResult(extracted_content=msg, include_in_memory=True)except Exception as e:logger.warning(f'Element not clickable with xpath {params.xpath} - most likely the page changed')@@ -184,16 +193,26 @@ class Controller(Generic[Context]):element_node = await browser.get_locate_element_by_text(text=params.text,nth=params.nth,+ element_type=params.element_type)if element_node:- await element_node.click()- msg = f'🖱️ Clicked on element with text {params.text}'+ try:+ await element_node.scroll_into_view_if_needed()+ await element_node.click(timeout=1500, force=True)+ except Exception as e:+ try:+ # Handle with js evaluate if fails to click using playwright+ await element_node.evaluate('el => el.click()')+ except Exception as e:+ logger.warning(f"Element not clickable with text '{params.text}' - {e}")+ return ActionResult(error=str(e))+ msg = f'🖱️ Clicked on element with text "{params.text}"'return ActionResult(extracted_content=msg, include_in_memory=True)else:return ActionResult(error=f"No element found for text '{params.text}'")except Exception as e:- logger.warning(f"Element not clickable with text {params.text} - {e}")+ logger.warning(f"Element not clickable with text '{params.text}' - {e}")return ActionResult(error=str(e))@self.registry.action(diff --git a/browser_use/controller/views.py b/browser_use/controller/views.pyindex 31c3bd833..1f8670b8a 100644--- a/browser_use/controller/views.py+++ b/browser_use/controller/views.py@@ -24,6 +24,7 @@ class ClickElementBySelectorAction(BaseModel):class ClickElementByTextAction(BaseModel):text: str+ element_type: Optional[str]nth: int = 0class InputTextAction(BaseModel):