Gets the XPath path of the HTML element

Source: Internet
Author: User
Tags tagname xpath

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Scriptsrc= "/jquery/jquery-1.11.1.min.js">5 </Script>6 <Script>7 8  functionReadxpath (Element) {9     if(Element.id! == ""){//determines the id attribute, if the element has an ID, displays//*[@id = "XPath"] form contentTen         return '//*[@id =\ "'+element.id+'\"]'; One     } A  -     if(Element.getattribute ("class")! == NULL){ //determines the class attribute, if the element has a class, displays//*[@class = "XPath"] form content -         return '//*[@class =\ "'+Element.getattribute ("class")+'\"]'; the     } -     //because the XPath attribute has more than one ID and class, you can add attributes in a more class form -  -     //The main string translation issues need to be needed here +  -  +     if(Element== document.body) {//recursion to body, end recursion A         return '/html/'+element.tagName.toLowerCase (); at     } -  -     varIX= 0,//position in NodeList, and initialize each click - siblings=Element.parentNode.childNodes;//child elements of a sibling -  in      for (varI= 0, L=siblings.length; I<l; I++) { -         varSibling=Siblings[i]; to         if(Sibling== Element) {//If this element is an element in the siblings array, the recursive operation is performed +             returnArguments.callee (Element.parentnode)+'/'+element.tagName.toLowerCase ()+(ix+1)==1?"':'['+(ix+1)+']');//Ix+1 is because the XPath is counted starting from 1, element.tagname+ ((ix+1) ==1? ': ' [' + (ix+1) + '] ') ternary operator, if the first one is not displayed, starting from 2 displays -         }Else if(Sibling.nodetype== 1 &&Sibling.tagname== element.tagname) {//If it is not, determine if it is an element and whether it is the same element, and if it is the same, start accumulating the IX++; *         } $     }Panax Notoginseng }; -  the $ (document). Ready (function () { +     varXPath= "', O; A     $('*'). Click (function(e) { the e.stoppropagation ();//Stop bubbling + o=  This; - alert (Readxpath (o)); $     }); $ }); -  - </Script> the </Head> - <Body>Wuyi <P>If you click on me, I will disappear.</P> the <P>Click on me and I will disappear.</P> - <P>Also to click on Me Oh.</P> Wu </Body> - </HTML>

Gets the XPath path of the HTML element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.