Skip to content

Instantly share code, notes, and snippets.

@sarangjaiswal
Last active August 11, 2021 03:27
Show Gist options
  • Save sarangjaiswal/1e89a7b18105728172c0 to your computer and use it in GitHub Desktop.
Save sarangjaiswal/1e89a7b18105728172c0 to your computer and use it in GitHub Desktop.
Find number of rows & columns in a webtable
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class LearningWebTable {
@Test
public void testWebTable(){
ProfilesIni profile = new ProfilesIni();
FirefoxProfile p = profile.getProfile("default");
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Users\\username\\AppData\\Local\\Mozilla Firefox\\firefox.exe"));
FirefoxDriver driver = new FirefoxDriver(binary, p);
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
driver.get("https://www.google.com/finance");
driver.findElement(By.xpath(".//*[@id='pHeader1']/td[1]/b/a")).click();
//Row Count in WebTable
WebElement table = driver.findElement(By.xpath("//table[@class='gf-table']"));
List<WebElement> row = table.findElements(By.tagName("tr"));
System.out.println("Total Number of Rows = " + row.size());
//Column Count in WebTable
List<WebElement> column = row.get(10).findElements(By.tagName("td"));
System.out.println("Total Number of Column = " + column.size());
System.out.println("==================================================================");
//Print content of Rows
for (int i=0;i<=row.size()-1 ;i++){
System.out.println(row.get(i).getText());
}
System.out.println("==================================================================");
// Print content of 2nd Column
List<WebElement> firstcol = driver.findElements(By.xpath("//table[@class='gf-table']/tbody/tr/td[2]"));
for (int j=0; j<=row.size();j++){
System.out.println(firstcol.get(j).getText());
}
}
}
@shriprasadbec
Copy link

row.get(10).findElements(By.tagName("td"));

why did you take row.get(10)? Is it row.get(1)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment