0

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

1 Answers1

0

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
  • WebElement table = driver.findElement(By.id("your_table_id")); Here do I need to add the table body id? – user2236394 Jul 02 '23 at 16:51
  • I need to get the value from the result(is in japanese character) web table, there are lets take 3 rows and 6 column in the web table result, from there It should take value of each row which has lets take column = 4 and 2. I need to get all the rows and find the value of all the column values = 4 and 2. Those column values I need to write it to an existing japanese csv file to a specific column let take column 4 in the csv file. Can anyone help me in this? I need this for selenium cucumber java, – user2236394 Jul 02 '23 at 16:57