Skip to content

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

Remove print

Magnus Müllerc1331604c7f6
diff --git a/browser_use/dom/debug/highlights.py b/browser_use/dom/debug/highlights.py
index 8e8a4893b..33beba22e 100644
--- a/browser_use/dom/debug/highlights.py
+++ b/browser_use/dom/debug/highlights.py
@@ -1,11 +1,14 @@
# 100% vibe coded
import json
+import logging
import traceback
from browser_use.dom.service import DomService
from browser_use.dom.views import DOMSelectorMap
+logger = logging.getLogger(__name__)
+
def convert_dom_selector_map_to_highlight_format(selector_map: DOMSelectorMap) -> list[dict]:
"""Convert DOMSelectorMap to the format expected by the highlighting script."""
@@ -55,7 +58,7 @@ async def remove_highlighting_script(dom_service: DomService) -> None:
# Get CDP client and session ID
cdp_session = await dom_service.browser_session.get_or_create_cdp_session()
- print('🧹 Removing browser-use highlighting elements')
+ logger.debug('🧹 Removing browser-use highlighting elements')
# Create script to remove all highlights
script = """
@@ -82,24 +85,22 @@ async def remove_highlighting_script(dom_service: DomService) -> None:
await cdp_session.cdp_client.send.Runtime.evaluate(
params={'expression': script, 'returnByValue': True}, session_id=cdp_session.session_id
)
- print('✅ All browser-use highlighting elements removed')
except Exception as e:
- print(f'❌ Error removing highlighting elements: {e}')
- traceback.print_exc()
+ traceback.logger.debug_exc()
async def inject_highlighting_script(dom_service: DomService, interactive_elements: DOMSelectorMap) -> None:
"""Inject JavaScript to highlight interactive elements with detailed hover tooltips that work around CSP restrictions."""
if not interactive_elements:
- print('⚠️ No interactive elements to highlight')
+ logger.debug('⚠️ No interactive elements to highlight')
return
try:
# Convert DOMSelectorMap to the format expected by the JavaScript
converted_elements = convert_dom_selector_map_to_highlight_format(interactive_elements)
- print(f'📍 Creating CSP-safe highlighting for {len(converted_elements)} elements')
+ logger.debug(f'📍 Creating CSP-safe highlighting for {len(converted_elements)} elements')
# ALWAYS remove any existing highlights first to prevent double-highlighting
await remove_highlighting_script(dom_service)
@@ -387,7 +388,7 @@ async def inject_highlighting_script(dom_service: DomService, interactive_elemen
// Add container to document
document.body.appendChild(container);
- console.log('✅ Browser-use highlighting complete');
+ console.log('Highlighting complete');
}})();
"""
@@ -397,8 +398,8 @@ async def inject_highlighting_script(dom_service: DomService, interactive_elemen
await cdp_session.cdp_client.send.Runtime.evaluate(
params={'expression': script, 'returnByValue': True}, session_id=cdp_session.session_id
)
- print(f'✅ Enhanced CSP-safe highlighting injected for {len(converted_elements)} elements')
+ logger.debug(f'Enhanced CSP-safe highlighting injected for {len(converted_elements)} elements')
except Exception as e:
- print(f'❌ Error injecting enhanced highlighting script: {e}')
+ logger.debug(f'❌ Error injecting enhanced highlighting script: {e}')
traceback.print_exc()
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.py
index aab1aa0e4..80932f6dc 100644
--- a/browser_use/tokens/service.py
+++ b/browser_use/tokens/service.py
@@ -130,7 +130,7 @@ class TokenCost:
cached = CachedPricingData.model_validate_json(content)
self._pricing_data = cached.data
except Exception as e:
- print(f'Error loading cached pricing data from {cache_file}: {e}')
+ logger.debug(f'Error loading cached pricing data from {cache_file}: {e}')
# Fall back to fetching
await self._fetch_and_cache_pricing_data()
@@ -157,7 +157,7 @@ class TokenCost:
await f.write(cached.model_dump_json(indent=2))
except Exception as e:
- print(f'Error fetching pricing data: {e}')
+ logger.debug(f'Error fetching pricing data: {e}')
# Fall back to empty pricing data
self._pricing_data = {}
@@ -559,7 +559,7 @@ class TokenCost:
except Exception:
pass
except Exception as e:
- print(f'Error cleaning old cache files: {e}')
+ logger.debug(f'Error cleaning old cache files: {e}')
async def ensure_pricing_loaded(self) -> None:
"""Ensure pricing data is loaded in the background. Call this after creating the service."""