Thursday, June 13, 2013

Handling Elements of a Webtable

The Code for Accessing a Cell value of a webtable would be like this. 

 driver.navigate().to(" Site which you want to recordl");
 WebElement table = driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div[2]/div[3]/table/tbody"));   // This would be the Xpath for locating the Element table in which tr and td exists
Collection<WebElement> table_Row_Object = table.findElements(By.tagName("tr"));
// The collection of Web Elements is collection all the WebElements with Tags tr in it. This is for table Object //only
WebElement table_Cell , table_Row ;
Collection<WebElement> tr_collection;
String Value_Cell;
 for ( Iterator<WebElement> i = table_Row_Object.iterator();i.hasNext(); )
        {
         table_Row = i.next();  // First Weblelement of tr is assigned
         tr_collection = table_Row.findElements(By.tagName("td")); //  Find all td in tr webelement .
         for ( Iterator<WebElement> j = tr_collection.iterator(); j.hasNext();)
         {
             table_Cell = j.next();
             Value_Cell = table_Cell.getText();
             System.out.println(Value_Cell); // And we won.... :)
          }
   }





No comments:

Post a Comment