Capture human-readable DOM tree structure. Unlike raw HTML, this provides a clean, indented tree view showing: - Element hierarchy - IDs and classes - Important attributes (disabled, value, etc.) Args: page: Playwright page instance Returns: str: Human
(page: AsyncPage)
| 72 | |
| 73 | |
| 74 | async def capture_dom_structure(page: AsyncPage) -> str: |
| 75 | """ |
| 76 | Capture human-readable DOM tree structure. |
| 77 | |
| 78 | Unlike raw HTML, this provides a clean, indented tree view showing: |
| 79 | - Element hierarchy |
| 80 | - IDs and classes |
| 81 | - Important attributes (disabled, value, etc.) |
| 82 | |
| 83 | Args: |
| 84 | page: Playwright page instance |
| 85 | |
| 86 | Returns: |
| 87 | str: Human-readable DOM tree structure |
| 88 | """ |
| 89 | try: |
| 90 | dom_tree = await page.evaluate("""() => { |
| 91 | function getTreeStructure(element, indent = '', depth = 0, maxDepth = 15) { |
| 92 | // Prevent infinite recursion |
| 93 | if (depth > maxDepth) { |
| 94 | return indent + '... (max depth reached)\\n'; |
| 95 | } |
| 96 | |
| 97 | let result = indent + element.tagName; |
| 98 | |
| 99 | // Add ID |
| 100 | if (element.id) { |
| 101 | result += `#${element.id}`; |
| 102 | } |
| 103 | |
| 104 | // Add classes |
| 105 | if (element.className && typeof element.className === 'string') { |
| 106 | const classes = element.className.trim().split(/\\s+/).filter(c => c); |
| 107 | if (classes.length > 0) { |
| 108 | result += '.' + classes.join('.'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Add important attributes |
| 113 | const importantAttrs = ['aria-label', 'type', 'role', 'data-test-id']; |
| 114 | for (const attr of importantAttrs) { |
| 115 | const val = element.getAttribute(attr); |
| 116 | if (val) { |
| 117 | result += ` [${attr}="${val}"]`; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Add state attributes |
| 122 | if (element.disabled !== undefined) { |
| 123 | result += ` [disabled=${element.disabled}]`; |
| 124 | } |
| 125 | if (element.hasAttribute('aria-disabled')) { |
| 126 | result += ` [aria-disabled=${element.getAttribute('aria-disabled')}]`; |
| 127 | } |
| 128 | |
| 129 | // Add value for input elements (truncated) |
| 130 | if (element.value && typeof element.value === 'string') { |
| 131 | const truncated = element.value.substring(0, 50); |