// Launching Browser
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://ifsc.bankifsccode.com/");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,600)");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id=\"ifsccode\"]")).click();
WebElement serachelement = driver.findElement(By.xpath("//*[@id=\"ifsccode\"]"));
// Read and Write Data Over Iteration
FileInputStream file = new FileInputStream(new File("D://ISFC_Code Update.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
FileOutputStream out = new FileOutputStream (new File("D://MICR.xlsx"));
try {
int rowCnt = sheet.getLastRowNum();
for (int i = 0; i <= rowCnt; i++) {
XSSFRow row = sheet.getRow(i);
XSSFCell cell = row.getCell(0);
if (cell.getStringCellValue().isEmpty()) {
break;
}
try {
serachelement.click();
serachelement.sendKeys(cell.getStringCellValue());
} catch (StaleElementReferenceException e) {
System.out.println("Stale Exception Caught");
serachelement = driver.findElement(By.xpath("//*[@id=\"ifsccode\"]"));
serachelement.click();
serachelement.sendKeys(cell.getStringCellValue());
e.printStackTrace();
}
WebElement submitelement = driver.findElement(By.xpath("//*[@id=\"table_form_1\"]/tbody/tr[2]/td/input[1]"));
submitelement.click();
Wait<WebDriver> wait2 = new FluentWait<WebDriver>(driver);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"ifsccode\"]")));
JavascriptExecutor js2 = (JavascriptExecutor) driver;
js2.executeScript("window.scrollBy(0,800)");
String micr = driver.findElement(By.xpath("/html/body/table[1]/tbody/tr[9]/td[2]/table/tbody/tr/td[1]/div/a[7]")).getText();
driver.get("https://ifsc.bankifsccode.com/");
JavascriptExecutor js1 = (JavascriptExecutor) driver;
js1.executeScript("window.scrollBy(0,600)");
Wait<WebDriver> wait3 = new FluentWait<WebDriver>(driver);
wait3.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"ifsccode\"]")));
int cellid = 1;
XSSFCell cell2 = row.createCell(cellid);
cell2.setCellValue(micr);
cell2.setCellType(Cell.CELL_TYPE_STRING);
Thread.sleep(2000);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
wb.write(out);
out.close();
file.close();
out.close();
driver.quit();
}
I am trying to resolve Stale Element Exception because it is terminating my application. For that i have tried try catch block, wait's, refresh web page, while loop but it is didn't work here may be it is wrong logic. I am not sure. I want to resolve this exception to smooth execution of application.