Friday 28 March 2014

Xpath | Selenium Uses - Part I



XPath, XML Path Language is a W3C standard that is used to navigate through elements and attributes in an XML document.


XPath Functions


     1| Conversion
               boolean( [object] )
               string( [object] )
               number( [object] )

     2| Math
               ceiling( number ) 
               floor( number )
               round( decimal )
               sum( node-set )

     3| Logic
               true()
               false()
               not( expr )

     4| Node
               lang(string)
               name([node-set])
               namespace-uri([node-set])

     5| Context
               count(node-set)
               last()
               position()

     6| String
               contains( haystack-string needle-string )
               concat( string1 string2 [stringn]* )
               normalize-space( string )
               starts-with(haystack needle) 
               string-length( [string] )
               substring(string start [length]) 
               substring-after(haystack needle) 
               substring-before(haystack needle)
               translate( string abc XYZ)


Both the Xpath Context and Xpath String are used for locating elements and does various functions on Selenium. The Functions mentioned below are based on the following URL, http://docs.python.org/2/library/re.html

Note|
For normalize-space() use the following link http://www.bing.com/search?q=asd+++asd&go=&qs=n&form=QBRE&filt=all&pq=asd+asd&sc=8-7&sp=-1&sk=


XPath Context


    count()

    //span[count(*)]
    //p[count(tt)]
    //*[count(*)]
    //dl[count(dt)]
    //div[count(dl)]
    /* all tables with 1 row and 2 cols e.g., //table[count(tr)=1 and count(tr/td)=2] */
    //div[count(dl)=1 and count(dl/dd)=1]

    last()

    //div[@id='module-contents']/dl[@class='function'][last()]
    //div[@id='module-contents']/dl[@class='function'][last()-1]

    position()

    //div[@id='module-contents']/dl[@class='function'][position()=4]    
    //div[@class='button' and position()=2]     Link
    //div[@class='button'][position()=2]



XPath String


    contains()

    //div[contains(@id,'module')]
    //*[@id='regular-expression-syntax']//h2[contains(text(), '7.2.1. Regular Expression Syntax')]

    concat()

    concat(//*[@id='checking-for-a-pair']/h3/text(), //*[@id='simulating-scanf']/h3/text())

    normalize-space()

    //*[@id='sb_form_q'][normalize-space(@value)='asd asd']    

    starts-with()

    //div[starts-with(@id,'module')]


    string-length()

     The string-length returns the number of characters in the string
     string-length(//*[@id='regular-expression-syntax']/h2/text())

    substring()

     substring(//*[@id='regular-expression-syntax']/h2/text(), 2, 30)

    substring-after()

    //div[substring-after(@id,'finding-')]
    //*[starts-with(@id,'finding') and substring-after(@id,'finding-all-adverbs-')]

    substring-before()

    //div[substring-before(@id,'objects')]

    translate()

    //*[@id='raw-string-notation']/h3[contains(translate(., '7.2.5.8. Raw String Notation', '7.2.5.8. RAW STRING NOTATION'), '7.2.5.8. RAW STRING NOTATION')]
    //*[@id='raw-string-notation']/h3[contains(translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '7.2.5.8. RAW STRING NOTATION')]

No comments:

Post a Comment