Selenium Tutorial: Locators Xpath

Download extension Xpath Helper in Chrome.

Link: https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

  1. “/” :  find from Root
  2.  “//” :  find anywhere in the Document
  3. “*”  : Find all combination
  4. Structure: //<TAG_NAME>[@<ATTRIBUTE> = ‘<ATT_VAL>’]
    1. Example: //Div[1]
    2. Example: //Div[@class = ‘abcd’]
  5. Relative vs Absolute path: http://parabank.parasoft.com/parabank/index.htm
    1. Absolute path: /html/body/div/div[3]/div[1]/div[1]/form/div[1]/input
    2. Relative path:
      1. Will return user name: //div[@class=’login’]/input[@name=’username’]
      2. Will return user name: //input[@name=’username’]
      3. Will return we all  input element: //div[@class=’login’]/*
    3. Find Element based on Text:
      1. Find link with text Read More: //a[text()=’Read More’]
    4. Find Attribute value: //a[text()=’Read More’]/@href

 

 

Assignment:

  1. Write Xpath to get text of all links: //a/text()
  2. Login in to paraBank;john/demo. Write xpath to get a list of all links inside Accounts table.
    1. //a[contains(@href,’parabank/activity’)]/text()
  3. Write XPath to give count of all the links inside account overview table.
    1. count(//table[@id=’accountTable’]//a)
  4. Write Xpath to give text of all the amount value:
    1. //table[@id=’accountTable’]//td[contains(text(),’$’)]/text()
    2. //table[@id=’accountTable’]//td[contains(text(),’$’)][1]/text() ‘ only first clm texts
    3. //table[@id=’accountTable’]//td[contains(text(),’$’)][2]/text() ‘ only second clm texts