Download extension Xpath Helper in Chrome.
Link: https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
- “/” : find from Root
- “//” : find anywhere in the Document
- “*” : Find all combination
- Structure: //<TAG_NAME>[@<ATTRIBUTE> = ‘<ATT_VAL>’]
- Example: //Div[1]
- Example: //Div[@class = ‘abcd’]
- Relative vs Absolute path: http://parabank.parasoft.com/parabank/index.htm
- Absolute path: /html/body/div/div[3]/div[1]/div[1]/form/div[1]/input
- Relative path:
- Will return user name: //div[@class=’login’]/input[@name=’username’]
- Will return user name: //input[@name=’username’]
- Will return we all input element: //div[@class=’login’]/*
- Find Element based on Text:
- Find link with text Read More: //a[text()=’Read More’]
- Find Attribute value: //a[text()=’Read More’]/@href
Assignment:
- Write Xpath to get text of all links: //a/text()
- Login in to paraBank;john/demo. Write xpath to get a list of all links inside Accounts table.
- //a[contains(@href,’parabank/activity’)]/text()
- Write XPath to give count of all the links inside account overview table.
- count(//table[@id=’accountTable’]//a)
- Write Xpath to give text of all the amount value:
- //table[@id=’accountTable’]//td[contains(text(),’$’)]/text()
- //table[@id=’accountTable’]//td[contains(text(),’$’)][1]/text() ‘ only first clm texts
- //table[@id=’accountTable’]//td[contains(text(),’$’)][2]/text() ‘ only second clm texts