Selenium Java - Click Element Inside Web Table

Selenium Java - Click Element Inside Web Table
Introduction
In this blog, we'll dive into using Selenium WebDriver with Java to work with web table elements. We'll show you how to find a specific row in a web table, click on a checkbox, and manage pagination smoothly. Let's get started!
Clicking an Element Inside a Web Table
Below is a complete Selenium code example that showcases how to navigate through a paginated web table, search for a specific entry, and click on a checkbox in the corresponding row.
java
package sa.Demo.SeleniumPractice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
public class Selenium_084 {
/****
* Sample Code Definition :- Selenium - WebTable - Click Element - Inside WebTable
* Developed By :- Pankaj Kumar
* Date of Creation :- 15-June-2024
* Project :- SAJVSAUTO - Java [Web / Mobile / API]
* SA_BLOG_CODE :- SAJVSAUTO_084
*/
@Test
public void Selenium_083() throws InterruptedException {
System.out.println("Selenium - WebTable - Print All Table Data");
// Open Chrome Browser
WebDriver driver = new ChromeDriver();
// Maximize the Browser Window
driver.manage().window().maximize();
// Setup the implicit Wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Open the URL
driver.get("https://practice.solveautomation.in");
// Wait for 5 Seconds
Thread.sleep(5000);
// Click on WebTable
driver.findElement(By.xpath("//a[normalize-space()='WebTable']")).click();
// Wait for 5 Seconds to observe the action (optional)
Thread.sleep(5000);
// Search for "India" and click on the checkbox
String strCountryName = "India";
int i = 1;
boolean isFound = false;
while (i < 19) {
// Find the table
WebElement table = driver.findElement(By.xpath("//table//tbody"));
// Get all rows of the table
List<WebElement> rows = table.findElements(By.tagName("tr"));
// Loop through rows and find the target
for (WebElement row : rows) {
List<WebElement> columns = row.findElements(By.tagName("td"));
String country = columns.get(1).getText();
if (strCountryName.equals(country)) {
columns.get(5).findElement(By.tagName("input")).click();
isFound = true; // Indicate that the item was found
break;
}
}
if (!isFound) {
// Click on Next Page
driver.findElement(By.xpath("//button[normalize-space()='>']")).click();
} else {
break;
}
i++;
}
// Wait for 5 Seconds to observe the action (optional)
Thread.sleep(5000);
// Quit the Driver
driver.quit();
}
public class CountryInfo {
private String country;
private String capital;
private String currency;
private String language;
// Constructor
public CountryInfo(String country, String capital, String currency, String language) {
this.country = country;
this.capital = capital;
this.currency = currency;
this.language = language;
}
// toString method for easy printing
@Override
public String toString() {
return "Country: " + country + ", Capital: " + capital + ", Currency: " + currency + ", Language: " + language;
}
}
}
Explanation
Browser Setup: Initializes ChromeDriver, maximizes the browser window, and sets an implicit wait.
Web Table Navigation:
Opens the target page and navigates to the web table section.
Handles pagination to search through multiple pages for the specified country.
Search and Interaction:
Loops through rows and columns of the table.
Finds the row that contains "India" and clicks the checkbox in the corresponding column.
Condition Handling:
Uses a flag (
isFound
) to break out of loops once the target is located.Ensures efficient navigation without redundant page transitions.
Driver Cleanup: Quits the WebDriver instance after completing the operation.
Conclusion
Using Selenium WebDriver with Java, you can easily work with elements inside web tables by combining search logic and pagination handling. This method is perfect for automating tasks that involve large datasets shown in tables.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
