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
refactor: consolidate PLACEHOLDER_4PX_SCREENSHOT constant
claude[bot]ba12148b1932
diff --git a/browser_use/agent/gif.py b/browser_use/agent/gif.pyindex 392929144..f79415cce 100644--- a/browser_use/agent/gif.py+++ b/browser_use/agent/gif.py@@ -8,6 +8,7 @@ import platformfrom typing import TYPE_CHECKINGfrom browser_use.agent.views import AgentHistoryList+from browser_use.browser.views import PLACEHOLDER_4PX_SCREENSHOTfrom browser_use.config import CONFIGif TYPE_CHECKING:@@ -117,11 +118,7 @@ def create_history_gif(# Find the first non-placeholder screenshot for the task framefirst_real_screenshot = Nonefor item in history.history:- if (- item.state.screenshot- and item.state.screenshot- != 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='- ):+ if item.state.screenshot and item.state.screenshot != PLACEHOLDER_4PX_SCREENSHOT:first_real_screenshot = item.state.screenshotbreak@@ -145,10 +142,7 @@ def create_history_gif(# Skip placeholder screenshots from about:blank pages# These are 4x4 white PNGs encoded as a specific base64 string- if (- item.state.screenshot- == 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='- ):+ if item.state.screenshot == PLACEHOLDER_4PX_SCREENSHOT:logger.debug(f'Skipping placeholder screenshot from about:blank page at step {i}')continuediff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex b068f952d..beee27db9 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -45,6 +45,7 @@ from browser_use.browser.types import (async_playwright,)from browser_use.browser.views import (+ PLACEHOLDER_4PX_SCREENSHOT,BrowserError,BrowserStateSummary,PageInfo,@@ -3610,7 +3611,7 @@ class BrowserSession(BaseModel):# not an exception because there's no point in retrying if we hit this, its always pointless to screenshot about:blank# raise ValueError('Refusing to take unneeded screenshot of empty new tab page')# return a 4px*4px white png to avoid wasting tokens - instead of 1px*1px white png that was- return 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='+ return PLACEHOLDER_4PX_SCREENSHOT# Always bring page to front before rendering, otherwise it crashes in some cases, not sure whytry:diff --git a/browser_use/browser/views.py b/browser_use/browser/views.pyindex 415ebb0a1..091728524 100644--- a/browser_use/browser/views.py+++ b/browser_use/browser/views.py@@ -6,6 +6,11 @@ from pydantic import BaseModelfrom browser_use.dom.history_tree_processor.service import DOMHistoryElementfrom browser_use.dom.views import DOMState+# Known placeholder image data for about:blank pages - a 4x4 white PNG+PLACEHOLDER_4PX_SCREENSHOT = (+ 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='+)+# Pydanticclass TabInfo(BaseModel):diff --git a/tests/ci/test_gif_filtering.py b/tests/ci/test_gif_filtering.pyindex db3d35d31..8c8450354 100644--- a/tests/ci/test_gif_filtering.py+++ b/tests/ci/test_gif_filtering.py@@ -2,20 +2,14 @@import base64import io-from pathlib import Pathimport pytestfrom PIL import Image-from browser_use import Agent, AgentHistoryList+from browser_use import AgentHistoryListfrom browser_use.agent.gif import create_history_gif-from browser_use.agent.views import AgentHistory, AgentOutput, ActionResult, StepMetadata-from browser_use.browser.views import BrowserStateHistory, TabInfo-from tests.ci.conftest import create_mock_llm---# Known placeholder image data for about:blank pages-PLACEHOLDER_4PX = 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='+from browser_use.agent.views import ActionResult, AgentHistory, AgentOutput+from browser_use.browser.views import PLACEHOLDER_4PX_SCREENSHOT, BrowserStateHistory, TabInfo@pytest.fixture@@ -57,7 +51,7 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):"""Test that 4px placeholder screenshots from about:blank pages are filtered out of GIFs."""# Create a history with mixed screenshots: real and placeholderhistory_items = []-+# First item: about:blank placeholder (should be filtered)history_items.append(AgentHistory(@@ -69,7 +63,7 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):),result=[ActionResult()],state=BrowserStateHistory(- screenshot=PLACEHOLDER_4PX,+ screenshot=PLACEHOLDER_4PX_SCREENSHOT,url='about:blank',title='New Tab',tabs=[TabInfo(page_id=1, url='about:blank', title='New Tab')],@@ -77,7 +71,7 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):),))-+# Second item: real screenshothistory_items.append(AgentHistory(@@ -97,7 +91,7 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):),))-+# Third item: another about:blank placeholder (should be filtered)history_items.append(AgentHistory(@@ -109,15 +103,15 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):),result=[ActionResult()],state=BrowserStateHistory(- screenshot=PLACEHOLDER_4PX,- url='about:blank',+ screenshot=PLACEHOLDER_4PX_SCREENSHOT,+ url='about:blank',title='New Tab',tabs=[TabInfo(page_id=2, url='about:blank', title='New Tab')],interacted_element=[None],),))-+# Fourth item: another real screenshothistory_items.append(AgentHistory(@@ -137,10 +131,10 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):),))-+# Create history listhistory = AgentHistoryList(history=history_items)-+# Generate GIFgif_path = test_dir / 'test_filtered.gif'create_history_gif(@@ -151,10 +145,10 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):show_goals=True,show_task=True,)-+# Verify GIF was createdassert gif_path.exists(), 'GIF was not created'-+# Open the GIF and check the frameswith Image.open(gif_path) as img:# Count frames@@ -167,14 +161,14 @@ async def test_gif_filters_out_placeholder_screenshots(test_dir):img.seek(img.tell() + 1)except EOFError:pass-+# We should have 3 frames total:# 1. Task frame (created from first real screenshot)- # 2. Second real screenshot+ # 2. Second real screenshot# 3. Fourth real screenshot# The two placeholder screenshots should be filtered outassert frame_count == 3, f'Expected 3 frames (1 task + 2 real screenshots), got {frame_count}'-+# All frames should have the same size (800x600), not 4x4for size in frame_sizes:assert size == (800, 600), f'Frame has incorrect size: {size}. Placeholder images may not have been filtered.'@@ -184,19 +178,19 @@ async def test_gif_handles_all_placeholders(test_dir):"""Test that GIF generation handles case where all screenshots are placeholders."""# Create a history with only placeholder screenshotshistory_items = []-+for i in range(3):history_items.append(AgentHistory(model_output=AgentOutput(evaluation_previous_goal='',memory='',- next_goal=f'Step {i+1}',+ next_goal=f'Step {i + 1}',action=[],),result=[ActionResult()],state=BrowserStateHistory(- screenshot=PLACEHOLDER_4PX,+ screenshot=PLACEHOLDER_4PX_SCREENSHOT,url='about:blank',title='New Tab',tabs=[TabInfo(page_id=1, url='about:blank', title='New Tab')],@@ -204,9 +198,9 @@ async def test_gif_handles_all_placeholders(test_dir):),))-+history = AgentHistoryList(history=history_items)-+# Generate GIF - should handle gracefullygif_path = test_dir / 'test_all_placeholders.gif'create_history_gif(@@ -215,10 +209,10 @@ async def test_gif_handles_all_placeholders(test_dir):output_path=str(gif_path),duration=500,)-+# With all placeholders filtered, no GIF should be createdassert not gif_path.exists(), 'GIF should not be created when all screenshots are placeholders'# Note: Removing the agent integration test due to sandbox issues in CI-# The unit tests above adequately verify the GIF filtering functionality\ No newline at end of file+# The unit tests above adequately verify the GIF filtering functionality