Home > Web Front-end > JS Tutorial > body text

How Can I Access Elements Using XPath in Selenium WebDriver with JavaScript?

Linda Hamilton
Release: 2024-11-18 05:19:02
Original
623 people have browsed it

How Can I Access Elements Using XPath in Selenium WebDriver with JavaScript?

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;
Copy after login

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]"));
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template