Sunday, September 27, 2015

Working on Webtables in Selenium with the use of Java

What is Webtable?
Web tables are basically group of elements that are logically stored in a row and column format. 
  1.  If we know that number or rows and Columns are fixed than we will use the below approach
1for(int numberOfRows=1; numberOfRows<=5; numberOfRows++)
2{
3for(int numberOfCol=1; numberOfCol <=3; numberOfCol++)
4{
5System.out.println(driver.findElement(By.xpath(“//div[@id='main']/table[1]/tbody/tr[“+numberOfRows+”]/th[“+numberOfCol+”]”)));
6}
7}

  1.      How to work with the dynamically changed webtables?
1WebElement htmltable=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
2List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
3 
4for(int rnum=0;rnum<rows.size();rnum++)
5{
6List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
7System.out.println("Number of columns:"+columns.size());
8 
9for(int cnum=0;cnum<columns.size();cnum++)
10{
11System.out.println(columns.get(cnum).getText());
12}
13}