如何将网页表格数据导出为CSV文件?Selenium代码实现求助
Let's pick up where you left off and finish building the functionality to export the table data to a CSV file. Here's a step-by-step breakdown with working code:
Step 1: Complete the Table Load Wait
First, finish the explicit wait to ensure the table is fully visible before extracting data. Replace your incomplete wait code with this (verify the table ID via browser dev tools if it changes):
// Wait for the data table to become visible WebElement table = new WebDriverWait(driver, 10) .until(ExpectedConditions.visibilityOfElementLocated(By.id("cross_rate_markets_stocks_1")));
Step 2: Extract Table Headers and Rows
Next, we'll collect the header row and all data rows from the table into a structured list of lists for easy CSV writing:
import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; // ... // Extract table headers List<WebElement> headerCells = table.findElement(By.tagName("thead")) .findElement(By.tagName("tr")) .findElements(By.tagName("th")); List<String> headers = new ArrayList<>(); for (WebElement cell : headerCells) { headers.add(cell.getText().trim()); } // Extract table data rows List<WebElement> rows = table.findElement(By.tagName("tbody")) .findElements(By.tagName("tr")); List<List<String>> tableData = new ArrayList<>(); tableData.add(headers); // Add headers as the first row for (WebElement row : rows) { List<WebElement> cells = row.findElements(By.tagName("td")); List<String> rowData = new ArrayList<>(); for (WebElement cell : cells) { rowData.add(cell.getText().trim()); } tableData.add(rowData); }
Step 3: Write Data to CSV File
You have two reliable options here—using a dedicated CSV library (recommended for edge cases) or plain Java:
Option A: Using OpenCSV (Recommended)
OpenCSV automatically handles edge cases like commas in cell text. First, add this Maven dependency to your project:
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.6</version> </dependency>
Then write the data to CSV:
import com.opencsv.CSVWriter; import java.io.FileWriter; import java.io.IOException; // ... try (CSVWriter writer = new CSVWriter(new FileWriter("indian_stocks.csv"))) { for (List<String> row : tableData) { writer.writeNext(row.toArray(new String[0])); } System.out.println("CSV file exported successfully!"); } catch (IOException e) { System.err.println("Error writing CSV file: " + e.getMessage()); e.printStackTrace(); }
Option B: Plain Java (No External Dependencies)
If you prefer not to add a library, use plain Java. We'll manually handle commas in cell text by wrapping those cells in quotes:
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; // ... try (PrintWriter writer = new PrintWriter(new File("indian_stocks.csv"))) { StringBuilder sb = new StringBuilder(); // Write headers for (String header : headers) { sb.append(header).append(","); } sb.deleteCharAt(sb.length() - 1); // Remove trailing comma sb.append("\n"); // Write data rows for (int i = 1; i < tableData.size(); i++) { List<String> row = tableData.get(i); for (String cell : row) { // Wrap cells with commas in double quotes if (cell.contains(",")) { sb.append("\"").append(cell).append("\"").append(","); } else { sb.append(cell).append(","); } } sb.deleteCharAt(sb.length() - 1); sb.append("\n"); } writer.write(sb.toString()); System.out.println("CSV file exported successfully!"); } catch (FileNotFoundException e) { System.err.println("Error writing CSV file: " + e.getMessage()); e.printStackTrace(); }
Step 4: Handle Pagination (Optional)
If the table spans multiple pages, add this loop to extract data from all pages:
// Loop through all pages until "Next" button is disabled while (driver.findElement(By.cssSelector(".pagination .next")).isEnabled()) { // Extract current page data (repeat Step 2 here) // ... // Click next page driver.findElement(By.cssSelector(".pagination .next")).click(); // Wait for table to reload new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(table)); table = new WebDriverWait(driver, 10) .until(ExpectedConditions.visibilityOfElementLocated(By.id("cross_rate_markets_stocks_1"))); }
Key Notes
- Locator Validation: Always double-check the table's ID/class using your browser's dev tools (F12) since website elements can change over time.
- Wait Strategy: Stick to explicit waits (like
WebDriverWait) instead of implicit waits to avoid flaky behavior. - Dynamic Content: If some cells load asynchronously, add extra explicit waits for those specific elements.
Content of the question originates from stack exchange, question author no one

