How to get the XPath for an element?

Question:

I am trying to programmatically get the XPath of an element of a webpage.

I am searching the HTML source for a specific pattern, when I find one, I get the returned string, and the start position of that string in the source.

What is the easiest and quickest way for me to then generate an XPath for that element based on its start position in the source?

Asked By: Milinekticker1

||

Answers:

you can try some extensions like selenium ide which give the x-path of html page.you can use the extension that by selecting the line..you can also record and play back the selected html tags.
https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd

Answered By: Ramesh

Here’s why asking for the XPath for a given part of an XML or HTML document is a mistake.

Character-based reference

If character-position indexing is truly ideal for your purpose, then treat your document as a string and access substrings via character positioning. In this framework, there will be a single way to specify subparts of the document — the starting and ending character position.

Markup-based reference

If your purpose is to specify a part of a document based on the names and values of its elements (tags) and attributes, then it makes sense to use XPath:

  • Asking for the XPath isn’t useful — there are many XPaths for any given document part.

  • A significant advantage of XPath is that it allows specification of a document part that can be invariant across different documents and changes to other parts of the document.

Markup such as XML and HTML associate higher-level structure with document parts beyond character positioning. XPath leverages that reference framework and allows a higher-level specification of document parts. Such references can be much more robust across both document instances and document changes.

Do not think in terms of the XPath but rather in terms of where in the space of element names, element values, attribute names, and attribute values the part of your document of interest resides. The ability to specify document parts in those terms is the power XPath provides.

Answered By: kjhughes
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.