diff --git a/user snippets/getElementByXpath.js b/user snippets/getElementByXpath.js index cb30988..9f62038 100644 --- a/user snippets/getElementByXpath.js +++ b/user snippets/getElementByXpath.js @@ -1,20 +1,14 @@ +function mablJavaScriptStep(mablInputs, callback, path = undefined) { + let element = getElementByXpath(path) + callback (element.innerText); +} /** - * Replace this with a detailed description of what your custom JS snippet does. - * @param {object} - mablInputs Object containing input - * variables (mablInputs.variables.user) - * @param {function} callback - The callback function + * Queries the page using the supplied Xpath (path variable) and returns the first element matching the provided xpath. + * This version of the snippet is an implementation of https://github.com/mablhq/mabl-javascript-snippets/blob/main/mabl%20functions/findFirstElementMatchingXpath.js + * element.innerText can be changed to element.click() to click on the element instead of returning a value. + * @param {String} path - Xpath to find the desired element + * @returns {Element} The found element */ -function mablJavaScriptStep(mablInputs, callback) { - /** - * Gets an element by XPath. - * @param {string} path - a valid XPath to the element - * @example - * let elementXpath = "//*[contains(text(),'match')]"; - * getElementByXpath(elementXpath); - * getElementByXpath(elementXPath).click(); - * getElementByXpath(elementXPath).innerText(); - */ - function getElementByXpath(path) { +function findFirstElementMatchingXpath(path) { return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; - } }