New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

get-xpath

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-xpath

Get the XPath from an HTML element

  • 3.3.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

npm (tag) Build Status License npm

get-xpath

📑 Extract the XPath of an HTML element

  • Works with browsers, NodeJS and DenoJS (JavaScript 5 and TypeScript)
  • No external dependencies
  • Unit-tested
  • Semantic Versioning

Install

npm i get-xpath

API

function getXPath( element: HTMLElement, options?: Partial< Options > ): string;

Where options is an optional object containing:

nametypedescription
ignoreIdbooleanDo not take elements' ID into account

Example:

<html>
    <body>
        <div id="x" ></div>
        <script>
            const div = document.getElementById( 'x' );
            const xpath1 = getXPath( div ); // returns '//*[@id="x"]'
            const xpath2 = getXPath( div, { ignoreId: true } ); // returns '/html/body/div'
        </script>
    </body>
</html>

Usage

Notes:

  • On Node or Deno, you have to provide a way to accessing or emulating the DOM. You can use JSDOM (or any other library) for that.
  • When using TypeScript, add the value "dom" to the property "lib"of your tsconfig.json. Otherwise you will probably get errors.

Browser

Global:

<script crossorigin src="https://unpkg.com/get-xpath" >
<script>
console.log(
    getXPath( document.getElementById( 'foo' ) )
);
</script>

ESM:

<script type="module" >
import getXPath from 'https://unpkg.com/get-xpath/index.esm.js';
console.log(
    getXPath( document.getElementById( 'foo' ) )
);
</script>

NodeJS

/// <reference lib="dom" />
const getXPath = require('get-xpath');
console.log(
    getXPath( document.getElementById( 'foo' ) )
);

Deno

/// <reference lib="dom" />
import getXPath from 'https://unpkg.com/get-xpath/index.esm.js';
console.log(
    getXPath( document.getElementById( 'foo' ) )
);

License

MIT © Thiago Delgado Pinto

Keywords

FAQs

Package last updated on 31 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc