Building XPaths



  • To get the number of elements under a list or table. We can use below selenium method with appropriate logic.
Method:   selenium.getXpathCount(path).intValue();
Returns:   Integer
Path     :    some of xpath examples
1.       //form/ul[contains(@id,'pageList')]/li[contains(@id, 'step')]
2.       "//div/table/tbody/descendant::tr"   -- descendants of table (no of rows)

  •  To retrieve an “attribute” value of any component:
Method:  selenium.getAttribute(path);
Returns:  String
Path:      xpath examples
1.       //form/ul[contains(@id,'pageList')]/li[contains(@id, 'step')]/@id
                                                ---   returns value of the list(li) attribute “id”
  • Xpath for label or any other components
·         //table/tbody/tr[1"]/td[2]/label[@for='1']
·         /h3[contains(text(),'blackboard')]
·         //input[@name='top_Next']
·         //input[@id='submit']


  • XPath for getting the values

for(int i=1;i<=3;i++)
{
        String val = selenium.getText("//dl[@id='problemList']/dt["+i+"]/label");
        System.out.println("val :"+val);
}

  • XPath for Checking the  radio button
selenium.check("//dl[@id='problemList']/dt[2]/label/input");