Accessing Elements by XPath in JavaScript for Selenium WebDriver
Getting the innerHTML of elements using JavaScript is necessary for WebDriver/Java since it cannot do it natively. However, not all elements have ID attributes for easy identification. Therefore, using a method like getElementByXpath can provide a solution.
To achieve this, you can employ the document.evaluate function:
document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
Here's how you can implement it:
function getElementByXpath(path) { return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; } console.log(getElementByXpath("//html[1]/body[1]/div[1]"));
The above is the detailed content of How Can I Access Elements Using XPath in Selenium WebDriver with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!