Mainly we use FirePath for getting XPath of an element but sometime we need to make XPath manually or need to customize the XPath.
Some Basic of XPath Notation
/
Select from the root node
ex: html/body/.......
//
Select nodes form the current node
ex: //table/tbody/tr/.............
@
Select attribute
ex: //input[@id='abc']
//input[@id='Some Value']
Select element with tag name and id
//input[contains(text(),'TextValue')]
Select element with text containing abc
ex: //input[contains(text(),'abc')]
//input[starts-with(text(),'TextValue')]
Select element with text starting with abc
ex: //input[starts-with(text(),'abc')]
//input[contains(@id, 'TextValue')]
identify element containing 'abc' in value attribute
//input[contains(@id, 'searchInput') and contains(@text,'google')]
look at two attributes in input node
//input[starts-with(@type, 'abc')]
find input node with attribute type and its value is starting with 'abc'
//*[@accesskey='abc']
Can use wild cards
//input[text()='abc']
Select element with tag input and text=abc
//td/a/strong[contains(text(),'abc')]
Find all elements containing 'abc'
.//input[position()=1]
Select Input of First Position
//div[@class='cmnts']/descendant::div[position()=2]
Select 2nd div of the div node One thing keep in mind that descendant means child, child of child etc
//div[@class='cmnts']/ancestor::div[position()=2]
//div[@class='GCUXF0KCICB']/ancestor-or-self::div[position()=2]
ancestor-or-self means traversing starts from current node.
//input[last()]
.//div[@class='GCUXF0KCO3B']/span[text()='Labels']
Select span text
.//div[count(*)=0]
Select div that does not contain any child node
Some Basic of XPath Notation
/
Select from the root node
ex: html/body/.......
//
Select nodes form the current node
ex: //table/tbody/tr/.............
@
Select attribute
ex: //input[@id='abc']
//input[@id='Some Value']
Select element with tag name and id
//input[contains(text(),'TextValue')]
Select element with text containing abc
ex: //input[contains(text(),'abc')]
//input[starts-with(text(),'TextValue')]
Select element with text starting with abc
ex: //input[starts-with(text(),'abc')]
//input[contains(@id, 'TextValue')]
identify element containing 'abc' in value attribute
//input[contains(@id, 'searchInput') and contains(@text,'google')]
look at two attributes in input node
//input[starts-with(@type, 'abc')]
find input node with attribute type and its value is starting with 'abc'
//*[@accesskey='abc']
Can use wild cards
//input[text()='abc']
Select element with tag input and text=abc
//td/a/strong[contains(text(),'abc')]
Find all elements containing 'abc'
.//input[position()=1]
Select Input of First Position
//div[@class='cmnts']/descendant::div[position()=2]
Select 2nd div of the div node One thing keep in mind that descendant means child, child of child etc
//div[@class='cmnts']/ancestor::div[position()=2]
//div[@class='GCUXF0KCICB']/ancestor-or-self::div[position()=2]
ancestor-or-self means traversing starts from current node.
//input[last()]
.//div[@class='GCUXF0KCO3B']/span[text()='Labels']
Select span text
.//div[count(*)=0]
Select div that does not contain any child node