How to Read and Write Data into Excel/CSV File | Read & Write File in Selenium
package com.ifsccode.reader;
import com.opencsv.CSVReader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.ifsccodebank.com/search-b...");
CSVReader reader = new CSVReader(new FileReader("D://IFSC//Read.csv"));
String csvCell[];
String separator = ",";
try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream("D://IFSC//Write.csv"));
Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
buffWriter.append("IFSCCode");
buffWriter.append(separator);
buffWriter.append("Bank");
buffWriter.append(separator);
buffWriter.append("Branch");
buffWriter.append(separator);
buffWriter.append("District");
buffWriter.append(separator);
buffWriter.append("State");
buffWriter.newLine();
buffWriter.flush();
while ((csvCell = reader.readNext()) != null) {
String ifscode = csvCell[0];
driver.findElement(By.id("txtIFSCCode")).sendKeys(ifscode);
driver.findElement(By.id("BC_btnSeach")).click();
String bank = driver.findElement(By.xpath("//*[@id=\"BC_GV\"]/tbody/tr/td/div[1]/div[1]/table/tbody/tr[2]/td[2]/b")).getText();
String branch = driver.findElement(By.xpath("//*[@id=\"BC_GV\"]/tbody/tr/td/div[1]/div[1]/table/tbody/tr[3]/td[2]/a")).getText();
String district = driver.findElement(By.xpath("//*[@id=\"BC_GV\"]/tbody/tr/td/div[1]/div[2]/div/table/tbody/tr[4]/td[2]/a")).getText();
String state = driver.findElement(By.xpath("//*[@id=\"BC_GV\"]/tbody/tr/td/div[1]/div[2]/div/table/tbody/tr[5]/td[2]/a")).getText();
driver.findElement(By.id("txtIFSCCode")).clear();
buffWriter.append(ifscode);
buffWriter.append(separator);
buffWriter.append(bank);
buffWriter.append(separator);
buffWriter.append(branch);
buffWriter.append(separator);
buffWriter.append(district);
buffWriter.append(separator);
buffWriter.append(state);
buffWriter.newLine();
buffWriter.flush();
}
}
}
}