Hi Everyone,
I just Nowpracticed one of the most commonly asked Selenium interview questions:
π βHow do you fetch all row data from a web table, excluding the header details?β
I implemented this using Java + Selenium WebDriver + TestNG in Eclipse IDE, and executed the script with Microsoft Edge browser.
π Step 1 β Script Preparation
Prepared a Selenium automation script in Java.
Used TestNG framework for execution and validation.
Goal: Fetch all table row data (only
π Code Snippet:
**import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class TableDataHandle {
@Test
public static void tableDataFetching() {
// Set system property for Edge browser
System.setProperty("webdriver.edge.driver",
"D:\\Eclipse-workspace\\SeleniumInterviewPractices\\Drivers\\msedgedriver.exe");
// Launch Edge browser
WebDriver driver = new EdgeDriver();
// Maximize window
driver.manage().window().maximize();
// Navigate to target webpage
driver.get("https://www.w3schools.com/html/html_tables.asp");
// Locate all rows (excluding header)
List<WebElement> rowData = driver.findElements(By.xpath("//table[@id='customers']/tbody/tr"));
// Iterate over rows and fetch all columns
for (WebElement row : rowData) {
List<WebElement> cols = row.findElements(By.tagName("td"));
for (WebElement col : cols) {
System.out.print(col.getText() + " ");
}
System.out.println();
}
// Close browser
driver.quit();
}
}**
π Step 2 β Browser Launch (Edge)
Script executed successfully with EdgeDriver.
*Microsoft Edge browser" launched and navigated to the target web page containing the table.
πΈ Screenshot β Edge Browser Launch
π Step 3 β Console Output
All table data (only
Verified results matched expected values.
πΈ Console Output Screenshot
β Step 4 β Validation with TestNG
Added TestNG assertions to validate successful table data fetch.
Validation Passed β Test case executed correctly.
πΈ TestNG Execution Summary Screenshot
π― Final Result
β Script executed successfully in Eclipse IDE.
β Edge browser launched and navigated correctly.
β Table data fetched and printed (headers excluded).
β TestNG test case passed.
β Captured screenshots for Edge browser, console output, and execution summary.
"π‘ Learning Outcome*
This practice helped me:
Understand table handling with Selenium.
Improve my TestNG validation and reporting.
Successfully run automation using Microsoft Edge browser.
Thanks for reading!