Please find list of ways by using which we can create xpath to identify WebElement in the DOM.
Here we have mentioned 15 different xpath for single web element (Society Name) from https://www.societyhive.com
- //input[@id=’txtSocietyName’]
- //input[@id=’txtSocietyName’ and @name=’societyName’]
- //input[@id=’txtSocietyName’ or @name=’societyName’] —Toggle switch
- //span[text()=’Please enter society name’]//preceding-sibling::input ——–Use of text() method
- //span[.=’Please enter society name’]//preceding-sibling::input —–Functionality of (.) is same as text(). Some time in chrome text() function does not work.So use (.)
- //span[contains(text(),’Please enter society name’)]//preceding-sibling::input –Use of contains()method
- //span[starts-with(text(),’Please enter society’)]//preceding-sibling::input ———Use of starts-with() method
- //div[@class=’col-socitysearch’]/..//input[@id=’txtSocietyName’]—-(/..) is used to go to parent element of current node/element.
- (//div[@class=’col-socitysearch’]/..//input)[1] ———Use of index if multiple element are available.
- (//div[@class=’col-socitysearch’]/..//input)[last()-1]—Use of last() function if we want to select last element of multiple showing result
- //input[@id=’txtCity’]//ancestor::div[ @class=’sh-banner-area’]//input[@name=’societyName’]—-Use of ancestor
- //div[@class=’col-socitysearch’]/parent::div//input[@id=’txtSocietyName’]————Use of parent
- //div[@class=’col-socitysearch’]//child::input —————-Use of child
- (//div[@class=’row sh-text-left’]//descendant::input)[1]——Use of descendant.
- //div[@class=’sh-banner-heading’]//following-sibling::div[1]//input[@id=’txtSocietyName’]—Use of following- sibling
- //span[contains(.,’Please enter society name’)]//preceding-sibling::input ———–Use of preceding -sibling.
Website :https://www.societyhive.com/