Select an XML element regardless of level with XPATH

后端 未结 4 1899
無奈伤痛
無奈伤痛 2021-02-05 06:48

I have this:


  
    text
  


  text

So I want

4条回答
  •  别那么骄傲
    2021-02-05 07:24

    W3Schools has really good free courses regarding everything that is HTML related. I highly recommend reading it and making the examples. https://www.w3schools.com/xml/xpath_intro.asp

    HINT: you can use your browser's console to evalute expressions. It's under Developer Tools - F12 key for Chorme and Firefox:

    $x('');
    

    So, as everyone said, you can use the // syntax to find an element anywhere in the page. e.g. //a will return you all a elements within the page.

    Most likely you will want a specific one, so, that's where you use the predicates. They are contained between brackets. Using this very page as example, here is a xquery:

    //a[text()="Simeon"]
    

    This xpath will return all a elements that have Simeon as it's text. In many situations you might need to improve you xpath to include more identifiers, be more specific.

    //a[text()="Simeon" AND @href="/users/274344/simeon"]
    

    So, you can use pretty much any HTML attribute, or even CSS to identify a specific node you want.

    But now, let´s take it up a notch. Let´s say you want to get the a element that is below user's mkimd answer from Jan 27. If you look at this page structure, you have to get you a and dive back a few levels, untill you are able to reach the span that holds the posting date.

    //a[text()="mkimd" AND ../../div[@class="user-action-time"]/span[contains(.,"Jan")]]
    

    There are many ways of doing these queries,this last example I gave can be achieved with different xqueries.

    I find that xqueries are very similar to navigation in directories in console, like Linux BASH - relative and absolute paths, and the identifiers are like SQL WHERE clauses.

    If you research, there are many functions available in XPATH syntax, such as

    • lower-case()
    • upper-case()
    • concat()
    • ends-with()
    • operators ( +,-,*,div,!=,<,<=,>, ...)

    I highly advise you to use some tool like Firefox Firefug FirePath addon to practice xquery and check if you are getting the element you want - it highlights the elements found.

    ====================

    EDIT - May-8th-15

    In case you are using Xpath in Selenium automation, know that it will not help you select CSS pseudo elements.

提交回复
热议问题