Print Table Data Using Selenium WebDriver

Selenium : Print Table Data

We can find every web element using its tag name. table has tr tag for row.
In Selenium WebDriver we can use driver.findElements(By.tagName(“tr”) to find all rows data.
Check below image and Selenium code for the same:

get table data using selenium webdriver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class printTableData {

public static void main(String[] args) throws InterruptedException {

//set GeckoDriver path for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Create Browser object
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(3000);

//Get Row Count
int rowCount = driver.findElements(By.tagName("tr")).size();

//Get Column Count
int colCount = driver.findElements(By.xpath("//thead//th")).size();

System.out.println("Row count :" + rowCount);
System.out.println("Col count :" + colCount);

//Print table Data

for (WebElement tdata: driver.findElements(By.tagName("tr"))) {
System.out.println(tdata.getText());
}
}

}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×