Skip to content

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

fix(context, service): streamline element visibility checks

satya-nutella0b0319bb831b
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 30e2f4b52..5b7e050ea 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -1530,8 +1530,8 @@ class BrowserContext:
# Use XPath to locate the element
element_handle = await current_frame.query_selector(f'xpath={xpath}')
if element_handle:
- is_hidden = await element_handle.is_hidden()
- if not is_hidden:
+ is_visible = await self._is_visible(element_handle)
+ if is_visible:
await element_handle.scroll_into_view_if_needed()
return element_handle
return None
@@ -1550,8 +1550,8 @@ class BrowserContext:
# Use CSS selector to locate the element
element_handle = await current_frame.query_selector(css_selector)
if element_handle:
- is_hidden = await element_handle.is_hidden()
- if not is_hidden:
+ is_visible = await self._is_visible(element_handle)
+ if is_visible:
await element_handle.scroll_into_view_if_needed()
return element_handle
return None
@@ -1589,8 +1589,8 @@ class BrowserContext:
else:
element_handle = elements[0]
- is_hidden = await element_handle.is_hidden()
- if not is_hidden:
+ is_visible = await self._is_visible(element_handle)
+ if is_visible:
await element_handle.scroll_into_view_if_needed()
return element_handle
except Exception as e:
@@ -1616,8 +1616,8 @@ class BrowserContext:
# Ensure element is ready for input
try:
await element_handle.wait_for_element_state('stable', timeout=1000)
- is_hidden = await element_handle.is_hidden()
- if not is_hidden:
+ is_visible = await self._is_visible(element_handle)
+ if is_visible:
await element_handle.scroll_into_view_if_needed(timeout=1000)
except Exception:
pass
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index ccbeba39f..c4fdc8d89 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -330,13 +330,20 @@ class Controller(Generic[Context]):
for locator in locators:
try:
- # First check if element exists and is visible
- if await locator.count() > 0 and await locator.first.is_visible():
- await locator.first.scroll_into_view_if_needed()
+ if await locator.count() == 0:
+ continue
+
+ element = await locator.first
+ is_visible = await element.is_visible()
+ bbox = await element.bounding_box()
+
+ if is_visible and bbox is not None and bbox['width'] > 0 and bbox['height'] > 0:
+ await element.scroll_into_view_if_needed()
await asyncio.sleep(0.5) # Wait for scroll to complete
msg = f'🔍 Scrolled to text: {text}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
+
except Exception as e:
logger.debug(f'Locator attempt failed: {str(e)}')
continue