Skip to main content
Glama
find-element.js1.33 kB
/** * Find an element using various selector strategies * * @param {Object} params * @param {string} params.selector - Element selector * @param {string} params.strategy - Selector strategy: 'css', 'xpath', or 'text' */ (function(params) { const { selector, strategy } = params; let element; if (strategy === 'text') { // Find element containing text const xpath = "//*[contains(text(), '" + selector + "')]"; const result = document.evaluate( xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); element = result.singleNodeValue; } else if (strategy === 'xpath') { // XPath selector const result = document.evaluate( selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); element = result.singleNodeValue; } else { // CSS selector (default) element = document.querySelector(selector); } if (element) { const outerHTML = element.outerHTML; // Truncate very long HTML to avoid overwhelming output const truncated = outerHTML.length > 5000 ? outerHTML.substring(0, 5000) + '...' : outerHTML; return 'Found element: ' + truncated; } return 'Element not found'; })

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hypothesi/mcp-server-tauri'

If you have feedback or need assistance with the MCP directory API, please join our Discord server