Skip to content

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

fix: reuse querySelectorAll result to avoid duplicate DOM traversal

MagMuellere600b70d453d
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index b32f2f218..44c205647 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -439,18 +439,19 @@ class DomService:
params={
'expression': """
(() => {
- // Skip on heavy pages — listener detection is too expensive
- if (document.querySelectorAll('*').length > 10000) {
+ // getEventListeners is only available in DevTools context via includeCommandLineAPI
+ if (typeof getEventListeners !== 'function') {
return null;
}
- // getEventListeners is only available in DevTools context via includeCommandLineAPI
- if (typeof getEventListeners !== 'function') {
+ const allElements = document.querySelectorAll('*');
+
+ // Skip on heavy pages — listener detection is too expensive
+ if (allElements.length > 10000) {
return null;
}
const elementsWithListeners = [];
- const allElements = document.querySelectorAll('*');
for (const el of allElements) {
try {