我正在尝试使用 Selenium 从站点下载 Excel 文件。
我这样做的方式:
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();
页面源字符串长度等于我从该站点手动下载的文件大小。
问题是我保存数据不正确,MS Excel 无法打开保存的文件。
如何正确保存文件?