Welcome to DOM Agent! This comprehensive guide will walk you through everything you need to know to effectively use DOM Agent for DOM inspection and AI-powered code generation.
DOM Agent is a powerful VS Code extension that bridges the gap between web development and AI-assisted coding. It allows you to:
- Capture and inspect any webpage's DOM structure in real-time
- Interact with elements through a visual interface
- Generate production-ready code using AI assistance
- Export element data in multiple formats for testing and automation
-
Install from Marketplace:
- Open VS Code/Cursor
- Go to Extensions (Ctrl+Shift+X)
- Search for "DOM Agent"
- Click Install
-
Alternative Installation:
code --install-extension dom-agent
- Open Command Palette (
Ctrl+Shift+P) - Run command:
DOM Agent: Open URL in DOM Agent - Enter a URL (e.g.,
https://github.comorlocalhost:3000) - Start inspecting! Click on elements in the captured DOM
Command Palette → DOM Agent: Open URL in DOM Agent
- Enter any valid URL
- Supports both HTTP and HTTPS
- Works with local development servers
Command Palette → DOM Agent: Detect Local Dev Server
- Automatically scans common development ports (3000, 3001, 4200, 5173, 8000, etc.)
- Displays framework information when detected
- Select from multiple running servers
- Move your mouse over elements in the captured DOM
- Elements highlight with a blue outline as you hover
- Click an element to select it permanently
- Selected elements show a red outline
When you select an element, DOM Agent displays:
- Tag Name: HTML element type (div, span, button, etc.)
- Classes: CSS class names
- ID: Element ID if present
- Attributes: All HTML attributes
- CSS Selectors: Multiple selector strategies
- XPath: Full XPath expression
- Computed Styles: Complete CSS styling information
- Select an element in the DOM view
- Run command:
DOM Agent: Generate Code from Selection - Choose framework: React, Vue, Angular, or vanilla JavaScript
- Copy generated code to your clipboard
React Components:
interface ButtonProps {
children: React.ReactNode;
onClick?: () => void;
variant?: 'primary' | 'secondary';
}
const Button: React.FC<ButtonProps> = ({
children,
onClick,
variant = 'primary'
}) => {
return (
<button
className={`btn btn-${variant}`}
onClick={onClick}
data-testid="button"
>
{children}
</button>
);
};Vue Components:
<template>
<button
:class="['btn', `btn-${variant}`]"
@click="handleClick"
data-testid="button"
>
<slot />
</button>
</template>
<script setup lang="ts">
interface Props {
variant?: 'primary' | 'secondary'
}
const props = withDefaults(defineProps<Props>(), {
variant: 'primary'
})
const emit = defineEmits<{
click: []
}>()
const handleClick = () => {
emit('click')
}
</script>Angular Components:
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-button',
template: `
<button
[ngClass]="['btn', 'btn-' + variant]"
(click)="onClick.emit()"
data-testid="button"
>
<ng-content></ng-content>
</button>
`,
styles: [`
.btn {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.btn-primary {
background-color: #007acc;
color: white;
}
`]
})
export class ButtonComponent {
@Input() variant: 'primary' | 'secondary' = 'primary';
@Output() onClick = new EventEmitter<void>();
}Access settings through:
File → Preferences → Settings → Extensions → DOM Agent
{
"domAgent.defaultBrowser": "chromium"
}chromium- Chrome/Chromium (recommended)firefox- Mozilla Firefoxwebkit- Safari WebKit
{
"domAgent.aiProvider": "cursor-native"
}cursor-native- Cursor IDE built-in AIopenai- OpenAI GPT modelsanthropic- Anthropic Claude models
{
"domAgent.autoDetectDevServer": true
}true- Automatically scan for dev serversfalse- Manual URL entry only
Add custom shortcuts in VS Code:
File → Preferences → Keyboard Shortcuts
Search for "DOM Agent" commands and assign your preferred key combinations.
DOM Agent generates multiple selector strategies for maximum reliability:
// Playwright
await page.locator('#submit-button').click();
// Cypress
cy.get('#submit-button').click();// Playwright
await page.getByTestId('submit-btn').click();
// Cypress
cy.get('[data-testid="submit-btn"]').click();// Playwright
await page.getByText('Submit').click();
// Cypress
cy.contains('Submit').click();// Playwright
await page.locator('.btn-primary').click();
// Cypress
cy.get('.btn-primary').click();// Playwright
await page.locator('[aria-label="Submit form"]').click();
// Cypress
cy.get('[aria-label="Submit form"]').click();DOM Agent also generates XPath expressions for complex scenarios:
// Absolute XPath
await page.locator('/html/body/div[1]/form/button').click();
// Relative XPath
await page.locator('//button[@type="submit"]').click();
// Advanced XPath with conditions
await page.locator('//button[contains(@class, "primary") and not(@disabled)]').click();For advanced users, you can customize AI prompts by modifying the extension settings:
{
"domAgent.customPrompts": {
"react": "Generate a React functional component with TypeScript...",
"vue": "Create a Vue 3 Composition API component...",
"angular": "Build an Angular component with reactive forms..."
}
}- Symptom: Commands not appearing in Command Palette
- Solution:
- Reload VS Code window:
Ctrl+Shift+P→ "Developer: Reload Window" - Check Node.js version (18+ required)
- Verify VS Code version (1.73.0+ required)
- Reload VS Code window:
- Symptom: "Failed to capture webpage" error
- Solution:
- Verify URL is accessible
- Check network connectivity
- Ensure Playwright browsers are installed
- Try different browser engine
- Symptom: Code generation returns empty or incorrect results
- Solution:
- Verify Cursor AI is enabled and configured
- Check API keys for alternative AI providers
- Ensure internet connectivity
- Try selecting a different element
- Symptom: Mouse hover doesn't highlight elements
- Solution:
- Refresh the DOM capture
- Check browser console for JavaScript errors
- Verify the webpage allows DOM manipulation
- Try a different URL
Enable debug logging for troubleshooting:
{
"domAgent.debugMode": true
}Check VS Code's output panel for detailed logs:
View → Output → DOM Agent
- Use Specific URLs: Instead of homepages, capture specific pages
- Limit DOM Depth: Configure maximum capture depth for large sites
- Choose Appropriate Browser: Chromium is fastest for most scenarios
- DOM Agent automatically cleans up resources
- Close unused webview panels to free memory
- Restart VS Code if experiencing memory issues
- Use local development servers when possible
- Avoid capturing large, media-heavy pages
- Consider network throttling for realistic testing
- Prefer Stable Selectors: Use IDs and test IDs when available
- Avoid Layout-dependent Selectors: Don't rely on CSS positioning
- Use Semantic HTML: Better selectors for accessible elements
- Review Generated Code: Always inspect AI-generated code
- Add Error Handling: Include proper error boundaries
- Follow Framework Conventions: Adhere to your team's coding standards
- Use Generated Selectors: Copy selectors directly to test files
- Maintain Test Data: Keep test IDs consistent across development
- Automate Selector Updates: Use DOM Agent for selector maintenance
- Commit Generated Code: Include AI-generated components in version control
- Document Selector Changes: Track selector updates in changelog
- Code Review: Review generated code for security and best practices
# Example GitHub Actions workflow
- name: Run DOM tests
run: |
npm install -g @playwright/test
npx playwright install
npx playwright test- Shared Selectors: Maintain a team selector library
- Consistent Naming: Use standardized test IDs and data attributes
- Documentation: Document testing strategies and selector conventions
- GitHub Issues: Report bugs
- Discussions: Ask questions
- Discord: Join community
- Email: support@dom-agent.dev
- Documentation: Full documentation
Now that you're familiar with DOM Agent basics:
- Explore Advanced Features: Try different AI providers and frameworks
- Customize Configuration: Tailor DOM Agent to your workflow
- Contribute: Help improve DOM Agent by reporting issues or contributing code
- Share Feedback: Let us know how DOM Agent can better serve your needs
Happy DOM inspection and code generation! 🚀