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
Enhances scroll functionality with pixel control
Daniel T.610419c9d7a3
diff --git a/browser_use/agent/system_prompt.md b/browser_use/agent/system_prompt.mdindex ed3d4a3e7..56f735e4d 100644--- a/browser_use/agent/system_prompt.md+++ b/browser_use/agent/system_prompt.md@@ -77,6 +77,7 @@ Strictly follow these rules while using the browser and navigating the web:- If research is needed, open a **new tab** instead of reusing the current one.- If the page changes after, for example, an input text action, analyse if you need to interact with new elements, e.g. selecting the right option from the list.- By default, only elements in the visible viewport are listed. Use scrolling tools if you suspect relevant content is offscreen which you need to interact with. Scroll ONLY if there are more pixels below or above the page. The extract content action gets the full loaded page content.+- You can scroll by a specific amount of pixels using the amount parameter, or scroll by one page height if no amount is specified.- If a captcha appears, attempt solving it if possible. If not, use fallback strategies (e.g., alternative site, backtrack).- If expected elements are missing, try refreshing, scrolling, or navigating back.- If the page is not fully loaded, use the wait action.diff --git a/browser_use/agent/system_prompt_no_thinking.md b/browser_use/agent/system_prompt_no_thinking.mdindex aadd944f9..218db95b7 100644--- a/browser_use/agent/system_prompt_no_thinking.md+++ b/browser_use/agent/system_prompt_no_thinking.md@@ -77,6 +77,7 @@ Strictly follow these rules while using the browser and navigating the web:- If research is needed, open a **new tab** instead of reusing the current one.- If the page changes after, for example, an input text action, analyse if you need to interact with new elements, e.g. selecting the right option from the list.- By default, only elements in the visible viewport are listed. Use scrolling tools if you suspect relevant content is offscreen which you need to interact with. Scroll ONLY if there are more pixels below or above the page. The extract content action gets the full loaded page content.+- You can scroll by a specific amount of pixels using the amount parameter, or scroll by one page height if no amount is specified.- If a captcha appears, attempt solving it if possible. If not, use fallback strategies (e.g., alternative site, backtrack).- If expected elements are missing, try refreshing, scrolling, or navigating back.- If the page is not fully loaded, use the wait action.diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 409c67437..479ca224d 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -462,7 +462,7 @@ Explain the content of the page and that the requested information is not availa# )@self.registry.action(- 'Scroll the page by one page (set down=True to scroll down, down=False to scroll up)',+ 'Scroll the page by specified amount in pixels (set down=True to scroll down, down=False to scroll up, amount=pixels to scroll or None for one page)',param_model=ScrollAction,)async def scroll(params: ScrollAction, browser_session: BrowserSession):@@ -472,15 +472,20 @@ Explain the content of the page and that the requested information is not availa"""page = await browser_session.get_current_page()- # Get window height with retries- dy_result, action_result = await retry_async_function(- lambda: page.evaluate('() => window.innerHeight'), 'Scroll failed due to an error.'- )- if action_result:- return action_result+ # Determine scroll amount+ if params.amount is not None:+ scroll_amount = params.amount+ else:+ # Get window height with retries (default behavior)+ dy_result, action_result = await retry_async_function(+ lambda: page.evaluate('() => window.innerHeight'), 'Scroll failed due to an error.'+ )+ if action_result:+ return action_result+ scroll_amount = dy_result or 0# Set direction based on down parameter- dy = dy_result if params.down else -(dy_result or 0)+ dy = scroll_amount if params.down else -scroll_amounttry:await browser_session._scroll_container(cast(int, dy))@@ -490,10 +495,16 @@ Explain the content of the page and that the requested information is not availalogger.debug('Smart scroll failed; used window.scrollBy fallback', exc_info=e)direction = 'down' if params.down else 'up'- msg = f'🔍 Scrolled {direction} the page by one page'+ if params.amount is not None:+ msg = f'🔍 Scrolled {direction} the page by {params.amount} pixels'+ long_term_memory = f'Scrolled {direction} the page by {params.amount} pixels'+ else:+ msg = f'🔍 Scrolled {direction} the page by one page'+ long_term_memory = f'Scrolled {direction} the page by one page'+logger.info(msg)return ActionResult(- extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled {direction} the page by one page'+ extracted_content=msg, include_in_memory=True, long_term_memory=long_term_memory)# send keysdiff --git a/browser_use/controller/views.py b/browser_use/controller/views.pyindex 50c4796ad..d1d9152b7 100644--- a/browser_use/controller/views.py+++ b/browser_use/controller/views.py@@ -46,6 +46,7 @@ class CloseTabAction(BaseModel):class ScrollAction(BaseModel):down: bool # True to scroll down, False to scroll up+ amount: int | None = Field(None, description='Number of pixels to scroll. If None, defaults to one page height')class SendKeysAction(BaseModel):diff --git a/examples/use-cases/scrolling_page.py b/examples/use-cases/scrolling_page.pyindex aaf754290..7615ad967 100644--- a/examples/use-cases/scrolling_page.py+++ b/examples/use-cases/scrolling_page.py@@ -18,10 +18,13 @@ if not os.getenv('OPENAI_API_KEY'):raise ValueError('OPENAI_API_KEY is not set')"""-Example: Using the 'Scroll down' action.+Example: Using the 'Scroll' action with custom amounts.-This script demonstrates how the agent can navigate to a webpage and scroll down the content.-If no amount is specified, the agent will scroll down by one page height.+This script demonstrates how the agent can navigate to a webpage and scroll by specific amounts.+The scroll action now supports:+- Scrolling by a specific number of pixels using the 'amount' parameter+- Scrolling by one page height if no amount is specified (default behavior)+- Scrolling up or down using the 'down' parameter"""llm = ChatOpenAI(model='gpt-4.1')@@ -30,8 +33,9 @@ browser_profile = BrowserProfile(headless=False)browser_session = BrowserSession(browser_profile=browser_profile)agent = Agent(- # task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll down by one page - then scroll up by 100 pixels - then scroll down by 100 pixels - then scroll down by 10000 pixels.",- task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll to the string 'The vast majority of computer'",+ task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll down by one page - then scroll up by 100 pixels - then scroll down by 100 pixels - then scroll down by 10000 pixels.",+ # Alternative task to demonstrate text-based scrolling:+ # task="Navigate to 'https://en.wikipedia.org/wiki/Internet' and scroll to the string 'The vast majority of computer'",llm=llm,browser_session=browser_session,)