I am trying to download Excel file from site using Selenium.
The way I’m doing it:
WebElement excelList = driver.findElement(By.xpath("..."));
excelList.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String pageSource = driver.getPageSource();
FileOutputStream fos = new FileOutputStream("d:/load.xls");
for (int i = 0; i < pageSource.length(); i++) {
char c = pageSource.charAt(i);
fos.write((byte) c);
}
fos.close();
The page source string length is equal to the file size that I downloaded manually from this site.
The problem is that I’m saving data incorrectly and MS Excel cannot open the saved file.
How can I save the file properly?
I figured it out.
All I need was to get input stream right from last page after clicking on load file button.
But method for getting page object ‘lastPage()’ has protected access.
Here is the way: