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
Refine viewport expansion logic and update documentation for clarity
Max Comperatorecf6d8af73e83
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.jsindex 2919ce6ab..7b8832edd 100644--- a/browser_use/dom/buildDomTree.js+++ b/browser_use/dom/buildDomTree.js@@ -756,12 +756,12 @@let isAnyRectInViewport = false;for (const rect of rects) {// Use the same logic as isInExpandedViewport check- if (rect.width > 0 && rect.height > 0 && !( // Only check non-empty rects+ if (viewportExpansion === -1 || (rect.width > 0 && rect.height > 0 && !( // Only check non-empty rectsrect.bottom < -viewportExpansion ||rect.top > window.innerHeight + viewportExpansion ||rect.right < -viewportExpansion ||rect.left > window.innerWidth + viewportExpansion- ) || viewportExpansion === -1) {+ ))) {isAnyRectInViewport = true;break;}@@ -827,8 +827,6 @@* Checks if an element is within the expanded viewport.*/function isInExpandedViewport(element, viewportExpansion) {- return true-if (viewportExpansion === -1) {return true;}@@ -850,7 +848,6 @@);}-// Check if *any* client rect is within the viewportfor (const rect of rects) {if (rect.width === 0 || rect.height === 0) continue; // Skip empty rectsdiff --git a/docs/customize/browser-settings.mdx b/docs/customize/browser-settings.mdxindex 13bdefeb3..3b7537930 100644--- a/docs/customize/browser-settings.mdx+++ b/docs/customize/browser-settings.mdx@@ -161,8 +161,10 @@ async def run_search():Highlight interactive elements on the screen with colorful bounding boxes.- **viewport_expansion** (default: `500`)- Viewport expansion in pixels. With this you can control how much of the page is included in the context of the LLM. If set to -1, all elements from the entire page will be included (this leads to high token usage). If set to 0, only the elements which are visible in the viewport will be included.- Default is 500 pixels, that means that we include a little bit more than the visible viewport inside the context.+ Viewport expansion in pixels. With this you can control how much of the page is included in the context of the LLM. Setting this parameter controls the highlighting of elements:+ - `-1`: All elements from the entire page will be included, regardless of visibility (highest token usage but most complete).+ - `0`: Only elements which are currently visible in the viewport will be included.+ - `500` (default): Elements in the viewport plus an additional 500 pixels in each direction will be included, providing a balance between context and token usage.### Restrict URLs