How to fetch for specific values ? Let’s take I have multiple rows and I want to get the each row value of specific column and write that value in to csv file on specific columns using selenium cucumber in java
I tried to loop no luck
How to fetch for specific values ? Let’s take I have multiple rows and I want to get the each row value of specific column and write that value in to csv file on specific columns using selenium cucumber in java
I tried to loop no luck
Just indentify rows and columns in dynamic table which you want to fetch,and then u can use loop, for reference look at below code snippet make necessary changes and you'll get it
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import com.opencsv.CSVWriter;
public class DynamicTableCSVExample {
public static void main(String[] args) throws IOException {
WebDriver driver = new ChromeDriver();
driver.get("your_page_url");
// Locate the table
WebElement table = driver.findElement(By.id("your_table_id"));
// Define the desired column index (starting from 0)
int desiredColumn = 3; // Column index
// Find all rows in the table
List<WebElement> rows = table.findElements(By.tagName("tr"));
// Create a CSVWriter instance to write to a CSV file
CSVWriter csvWriter = new CSVWriter(new FileWriter("output.csv"));
for (WebElement row : rows) {
// Find all cells in the current row
List<WebElement> cells = row.findElements(By.tagName("td"));
if (!cells.isEmpty()) {
// Locate the desired cell in the current row using column index
WebElement cell = cells.get(desiredColumn);
// Get the cell value
String cellValue = cell.getText();
System.out.println("Cell Value: " + cellValue);
// Write the cell value to the CSV file
csvWriter.writeNext(new String[] { cellValue });
}
}
// Close the CSVWriter
csv