Skip to content

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

Apply Ruff formatting

Guido Trevisan2c7dd75bf0a8
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index dc1729318..810f0f160 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -1910,12 +1910,12 @@ class BrowserContext:
pixels_above = scroll_y
pixels_below = total_height - (scroll_y + viewport_height)
return pixels_above, pixels_below
-
+
async def _scroll_container(self, pixels: int) -> None:
"""Scroll the element that truly owns vertical scroll.Starts at the focused node ➜ climbs to the first big, scroll-enabled ancestor otherwise picks the first scrollable element or the root, then calls `element.scrollBy` (or `window.scrollBy` for the root) by the supplied pixel value."""
-
+
page = await self.get_agent_current_page()
-
+
# An element can *really* scroll if: overflow-y is auto|scroll|overlay, it has more content than fits, its own viewport is not a postage stamp (more than 50 % of window).
SMART_SCROLL_JS = """(dy) => {
const bigEnough = el => el.clientHeight >= window.innerHeight * 0.5;
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index 0290cf396..0634ccb97 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -252,49 +252,45 @@ class Controller(Generic[Context]):
return ActionResult(extracted_content=msg)
@self.registry.action(
- "Scroll down the page by pixel amount - if none is given, scroll one page",
- param_model=ScrollAction,
- )
+ 'Scroll down the page by pixel amount - if none is given, scroll one page',
+ param_model=ScrollAction,
+ )
async def scroll_down(params: ScrollAction, browser: BrowserContext):
"""
(a) Use browser._scroll_container for container-aware scrolling.
(b) If that JavaScript throws, fall back to window.scrollBy().
"""
page = await browser.get_current_page()
- dy = params.amount or await page.evaluate("() => window.innerHeight")
-
+ dy = params.amount or await page.evaluate('() => window.innerHeight')
try:
await browser._scroll_container(dy)
except Exception as e:
# Hard fallback: always works on root scroller
- await page.evaluate("(y) => window.scrollBy(0, y)", dy)
- logger.debug("Smart scroll failed; used window.scrollBy fallback", exc_info=e)
+ await page.evaluate('(y) => window.scrollBy(0, y)', dy)
+ logger.debug('Smart scroll failed; used window.scrollBy fallback', exc_info=e)
-
- amount_str = f"{params.amount} pixels" if params.amount is not None else "one page"
- msg = f"🔍 Scrolled down the page by {amount_str}"
+ amount_str = f'{params.amount} pixels' if params.amount is not None else 'one page'
+ msg = f'🔍 Scrolled down the page by {amount_str}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
@self.registry.action(
- "Scroll up the page by pixel amount - if none is given, scroll one page",
+ 'Scroll up the page by pixel amount - if none is given, scroll one page',
param_model=ScrollAction,
)
async def scroll_up(params: ScrollAction, browser: BrowserContext):
page = await browser.get_current_page()
- dy = -(params.amount or await page.evaluate("() => window.innerHeight"))
-
+ dy = -(params.amount or await page.evaluate('() => window.innerHeight'))
try:
await browser._scroll_container(dy)
except Exception as e:
- await page.evaluate("(y) => window.scrollBy(0, y)", dy)
- logger.debug("Smart scroll failed; used window.scrollBy fallback", exc_info=e)
-
+ await page.evaluate('(y) => window.scrollBy(0, y)', dy)
+ logger.debug('Smart scroll failed; used window.scrollBy fallback', exc_info=e)
- amount_str = f"{params.amount} pixels" if params.amount is not None else "one page"
- msg = f"🔍 Scrolled up the page by {amount_str}"
+ amount_str = f'{params.amount} pixels' if params.amount is not None else 'one page'
+ msg = f'🔍 Scrolled up the page by {amount_str}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)