Skip to content

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

added event listeners to build dom tree

Gregor Žunič4891513f4df6
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.js
index 8437f7cf7..0d655042e 100644
--- a/browser_use/dom/buildDomTree.js
+++ b/browser_use/dom/buildDomTree.js
@@ -704,6 +704,28 @@
if (hasInteractiveRole) return true;
+ // check whether element has event listeners
+ try {
+ if (typeof getEventListeners === 'function') {
+ const listeners = getEventListeners(element);
+ const mouseEvents = ['click', 'mousedown', 'mouseup', 'dblclick'];
+ for (const eventType of mouseEvents) {
+ if (listeners[eventType] && listeners[eventType].length > 0) {
+ return true; // Found a mouse interaction listener
+ }
+ }
+ } else {
+ // Fallback: Check common event attributes if getEventListeners is not available
+ const commonMouseAttrs = ['onclick', 'onmousedown', 'onmouseup', 'ondblclick'];
+ if (commonMouseAttrs.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
+ }
+
return false
}
diff --git a/browser_use/dom/tests/extraction_test.py b/browser_use/dom/tests/extraction_test.py
index 7c0f8aa92..135f18404 100644
--- a/browser_use/dom/tests/extraction_test.py
+++ b/browser_use/dom/tests/extraction_test.py
@@ -106,6 +106,7 @@ async def test_process_html_file():
TIMEOUT = 60
DEFAULT_INCLUDE_ATTRIBUTES = [
+ 'id',
'title',
'type',
'name',
@@ -134,8 +135,8 @@ async def test_focus_vs_all_elements():
context = BrowserContext(browser=browser, config=config) # noqa: F821
websites = [
- 'https://kayak.com/flights',
- 'https://en.wikipedia.org/wiki/Humanist_Party_of_Ontario',
+ # 'https://kayak.com/flights',
+ # 'https://en.wikipedia.org/wiki/Humanist_Party_of_Ontario',
# 'https://www.google.com/travel/flights?tfs=CBwQARoJagcIARIDTEpVGglyBwgBEgNMSlVAAUgBcAGCAQsI____________AZgBAQ&tfu=KgIIAw&hl=en-US&gl=US',
# # 'https://www.concur.com/?&cookie_preferences=cpra',
'https://immobilienscout24.de',