Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Webdriverio Webdriverio XPath PageSourceExecutor

From Leeroopedia
Knowledge Sources
Domains Mobile_Testing, XML_Processing
Last Updated 2026-02-12 00:00 GMT

Overview

The XPath PageSourceExecutor module executes XPath expressions against page source XML using a DOM parser and returns structured element data.

Description

This module provides two functions for XPath execution on XML page source. executeXPathOnPageSource parses the page source XML using @xmldom/xmldom's DOMParser, executes the XPath expression using the xpath library, and returns an array of ElementData objects containing the element type (node name) and all attributes as key-value pairs. findElementByXPathWithFallback wraps the execution function to return the first matching element along with the total match count, providing the data needed for uniqueness validation in the selector builder. Parse errors and execution failures are handled gracefully with null returns.

Usage

These functions are called by the XPath converter to find elements in the page source DOM, enabling the selector builder to construct optimized native selectors from the element's actual attributes.

Code Reference

Source Location

Signature

export function executeXPathOnPageSource(
    xpathExpr: string,
    pageSource: string
): ElementData[] | null

export function findElementByXPathWithFallback(
    xpathExpr: string,
    pageSource: string
): XPathExecutionResult | null

Import

import { executeXPathOnPageSource, findElementByXPathWithFallback } from './utils/xpath-page-source-executor.js'

I/O Contract

Inputs

Name Type Required Description
xpathExpr string Yes XPath expression to execute against the page source DOM
pageSource string Yes Raw XML page source string from the Appium driver

Outputs

Name Type Description
executeXPathOnPageSource return null Array of element data objects (type + attributes), empty array if no matches, or null on parse/execution error
findElementByXPathWithFallback return null Object with element (first matching ElementData) and matchCount (total matches), or null if no elements found

Usage Examples

Executing XPath on Page Source

import { executeXPathOnPageSource } from './xpath-page-source-executor.js'

const pageSource = `
<AppiumAUT>
  <XCUIElementTypeWindow>
    <XCUIElementTypeButton name="Login" label="Login" visible="true" />
    <XCUIElementTypeButton name="Cancel" label="Cancel" visible="true" />
  </XCUIElementTypeWindow>
</AppiumAUT>`

const elements = executeXPathOnPageSource(
    '//XCUIElementTypeButton[@name="Login"]',
    pageSource
)
// [{
//     type: 'XCUIElementTypeButton',
//     attributes: { name: 'Login', label: 'Login', visible: 'true' }
// }]

Finding Element with Match Count

import { findElementByXPathWithFallback } from './xpath-page-source-executor.js'

const result = findElementByXPathWithFallback(
    '//XCUIElementTypeButton',
    pageSource
)
// {
//     element: { type: 'XCUIElementTypeButton', attributes: { name: 'Login', label: 'Login', visible: 'true' } },
//     matchCount: 2
// }

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment