Sunday, May 29, 2016

XPath techniques


    multiple condition: //div[@class='bubble-title' and contains(text(), 'Cover')]
    partial match: //span[contains(text(), 'Assign Rate')]
    starts-with: //input[starts-with(@id,'reportcombo')
    value has spaces: //div[./div/div[normalize-space(.)='More Actions...']]
    sibling: //td[.='LoadType']/following-sibling::td[1]/select"
    more complex: //td[contains(normalize-space(@class), 'actualcell sajcell-row-lines saj-special x-grid-row-collapsed')]
to handle case sensitivity  //td[contains(lower-case(text()),'youruser')]")  

xpath: find table cell with same position in different row
http://stackoverflow.com/questions/18960462/xpath-find-table-cell-with-same-position-in-different-row

//td[. = 'Label 2']/../following-sibling::tr/td[position() = count(//td[. = 'Label 2']/preceding-sibling::td)+1]/text()


//th[. = 'Last Name']/../following-sibling::tr/td[position() = count(//th[. = 'Last Name']/preceding-sibling::td)+1]/text()

get td of tr row by column heading (that uses th instead of tr)
sample site used http://www.w3schools.com/html/html_tables.asp
//th[. = 'Last Name']/../following-sibling::tr/td[position() = count(//th[. = 'Last Name']/preceding-sibling::th)+1]/text()

or

//th[. = 'Last Name']/../following-sibling::tr/td[count(//th[. = 'Last Name']/preceding-sibling::th)+1]/text()


gets values in column 'Last Name'
Jackson
Doe
Johnson
Smith



//th[. = 'Last Name']/../following-sibling::tr/td[1] and the below are same
//th[. = 'Last Name']/../following-sibling::tr/td[position() = 1]


get last in node set
//th[. = 'Last Name']/../following-sibling::tr/td[position() = last()]


get count off all td elements; this gets the value even in FireBug :)
count(//td)

Get the [column] position ( or index) of an element 
count(//th[. = 'Last Name']/preceding-sibling::*)+1
or
count(//th[. = 'Last Name']/preceding-sibling::th)+1
        |                                      |
        |   These 2 element should match       |
        ----------------------------------------



get td of tr row by column heading (that uses th instead of tr)
sample site used http://www.w3schools.com/html/html_tables.asp
//th[. = 'Last Name']/../following-sibling::tr/td[position() = count(//th[. = 'Last Name']/preceding-sibling::th)+1]/text()

gets values in column 'Last Name'
Jackson
Doe
Johnson
Smith

------------------------
Refer any element using an it's label as seen in the user page as ANCHOR point and use descendant operator descendant:: or // to avoid in-between path elements

//div[contains(text(),'User Name')]//input

note the // before input tag

-----------------------

Case in-sensitive XPATH

To locate a tag with title='Gift With Purchase' as well as title='GIFT WITH PURCHASE'
//a[contains(translate(@title, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'GIFT WITH PURCHASE')]
or 
//a[.='GIFT WITH PURCHASE' or .='Gift With Purchase']

The translate function changes every character returned by @title attribute that matches in any of 'abcdefghijklmnopqrstuvwxyz' to the corresponding character at SAME POSITION in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-----------------------

No comments:

Post a Comment