MCPcopy Create free account
hub / github.com/InverseUI/InverseUI-Recorder / getPlaywrightSelector

Function getPlaywrightSelector

util/playwright_selector.js:555–584  ·  view source on GitHub ↗
(element)

Source from the content-addressed store, hash-verified

553 * Prioritizes unique selectors over non-unique ones
554 */
555export function getPlaywrightSelector(element) {
556 if (!element) {
557 return { method: 'locator', selector: '*' };
558 }
559
560 // Build all candidates
561 const candidates = buildCandidates(element);
562
563 // Sort by score (lower is better)
564 candidates.sort((a, b) => a.score - b.score);
565
566 // Find the best UNIQUE candidate
567 for (const candidate of candidates) {
568 const count = countMatchingElements(candidate);
569 if (count === 1) {
570 const { score, ...result } = candidate;
571 return result;
572 }
573 }
574
575 // No unique candidate found - prefer ID if available (even if GUID-like)
576 const idAttr = element.getAttribute('id');
577 if (idAttr) {
578 return { method: 'locator', selector: `#${CSS.escape(idAttr)}` };
579 }
580
581 // Fallback to best candidate even if not unique
582 const { score, ...result } = candidates[0];
583 return result;
584}
585
586// Keep backward compatibility with old function name
587export function getPlaywrightMethod(element) {

Callers 5

handleInputMethod · 0.90
handleChangeMethod · 0.90
handleKeyDownMethod · 0.90
handleClickMethod · 0.90
getPlaywrightMethodFunction · 0.85

Calls 2

buildCandidatesFunction · 0.85
countMatchingElementsFunction · 0.85

Tested by

no test coverage detected