简体   繁体   中英

How to get the XPath for an element?

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?

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 variations or 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM