Skip to content

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

fix: fixing interactive element event checking: 1. add event_listeners_script 2. adding getEventListenersForNode in buildDomTree.js for checking element event

mingzhong.lic0a936e54ce1
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 2497460e7..90d80743d 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -566,6 +566,60 @@ class BrowserSession(BaseModel):
"""
await self.browser_context.add_init_script(update_tab_focus_script)
+ event_listeners_init_script = """
+ // check to make sure we're not inside the PDF viewer
+ window.isPdfViewer = !!document?.body?.querySelector('body > embed[type="application/pdf"][width="100%"]')
+ if (!window.isPdfViewer) {
+
+ // Permissions
+ const originalQuery = window.navigator.permissions.query;
+ window.navigator.permissions.query = (parameters) => (
+ parameters.name === 'notifications' ?
+ Promise.resolve({ state: Notification.permission }) :
+ originalQuery(parameters)
+ );
+ (() => {
+ if (window._eventListenerTrackerInitialized) return;
+ window._eventListenerTrackerInitialized = true;
+
+ const originalAddEventListener = EventTarget.prototype.addEventListener;
+ const eventListenersMap = new WeakMap();
+
+ EventTarget.prototype.addEventListener = function(type, listener, options) {
+ if (typeof listener === "function") {
+ let listeners = eventListenersMap.get(this);
+ if (!listeners) {
+ listeners = [];
+ eventListenersMap.set(this, listeners);
+ }
+
+ listeners.push({
+ type,
+ listener,
+ listenerPreview: listener.toString().slice(0, 100),
+ options
+ });
+ }
+
+ return originalAddEventListener.call(this, type, listener, options);
+ };
+
+ window.getEventListenersForNode = (node) => {
+ console.log(eventListenersMap)
+ const listeners = eventListenersMap.get(node) || [];
+ return listeners.map(({ type, listenerPreview, options }) => ({
+ type,
+ listenerPreview,
+ options
+ }));
+ };
+ })();
+ }
+ """
+
+ # Expose anti-detection scripts
+ await self.browser_context.add_init_script(event_listeners_init_script)
+
# Set up visibility listeners for all existing tabs
for page in self.browser_context.pages:
try:
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 03ad45be8..1abf4ac4e 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -834,15 +834,27 @@
return true; // Found a mouse interaction listener
}
}
- } else {
- // Fallback: Check common event attributes if getEventListeners is not available (getEventListeners doesn't work in page.evaluate context)
+ }
+
+ const getEventListenersForNode = window.getEventListenersForNode;
+ if (typeof getEventListenersForNode === 'function') {
+ const listeners = getEventListenersForNode(element);
+ const interactionEvents = ['click', 'mousedown', 'mouseup', 'keydown', 'keyup', 'submit', 'change', 'input', 'focus', 'blur'];
+ for (const eventType of interactionEvents) {
+ for (const listener of listeners) {
+ if (listener.type === eventType) {
+ return true; // Found a common interaction listener
+ }
+ }
+ }
+ }
+ // Fallback: Check common event attributes if getEventListeners is not available (getEventListeners doesn't work in page.evaluate context)
const commonMouseAttrs = ['onclick', 'onmousedown', 'onmouseup', 'ondblclick'];
for (const attr of commonMouseAttrs) {
if (element.hasAttribute(attr) || typeof element[attr] === 'function') {
return true;
}
}
- }
} catch (e) {
// console.warn(`Could not check event listeners for ${element.tagName}:`, e);
// If checking listeners fails, rely on other checks
@@ -1115,10 +1127,28 @@
return true;
}
- // Check common event attributes (getEventListenersForNode doesn't work in page.evaluate context)
- const commonEventAttrs = ['onmousedown', 'onmouseup', 'onkeydown', 'onkeyup', 'onsubmit', 'onchange', 'oninput', 'onfocus', 'onblur'];
- if (commonEventAttrs.some(attr => element.hasAttribute(attr))) {
- return true;
+ // Check for other common interaction event listeners
+ try {
+ const getEventListenersForNode = window.getEventListenersForNode;
+ if (typeof getEventListenersForNode === 'function') {
+ const listeners = getEventListenersForNode(element);
+ const interactionEvents = ['click', 'mousedown', 'mouseup', 'keydown', 'keyup', 'submit', 'change', 'input', 'focus', 'blur'];
+ for (const eventType of interactionEvents) {
+ for (const listener of listeners) {
+ if (listener.type === eventType) {
+ return true; // Found a common interaction listener
+ }
+ }
+ }
+ }
+ // Fallback: Check common event attributes if getEventListeners is not available (getEventListenersForNode doesn't work in page.evaluate context)
+ const commonEventAttrs = ['onmousedown', 'onmouseup', 'onkeydown', 'onkeyup', 'onsubmit', 'onchange', 'oninput', 'onfocus', 'onblur'];
+ if (commonEventAttrs.some(attr => element.hasAttribute(attr))) {
+ return true;
+ }
+ } catch (e) {
+ // console.warn(`Could not check event listeners for ${element.tagName}:`, e);
+ // If checking listeners fails, rely on other checks
}
// if the element is not strictly interactive but appears clickable based on heuristic signals