fix: 修复导出下载 — 使用 waitForDownload 并输出绝对路径
- 用 page.waitForDownload 替代 onDownload + sleep,正确捕获下载事件 - 增加 5 分钟超时,等待服务器生成大文件 - 输出文件绝对路径和大小,不再找不到下载文件 - 只触发一次下载,不再重复 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
68d0cca25a
commit
ea0f566e12
@ -142,33 +142,31 @@ public class EtsScraper {
|
|||||||
|
|
||||||
screenshot(page, "after_query");
|
screenshot(page, "after_query");
|
||||||
|
|
||||||
// 点击导出按钮
|
// 点击导出按钮(先弹出导出选项对话框,再点击对话框内的导出)
|
||||||
boolean exportBtnExists = page.locator("#Export_ThreeBillList_Button").count() > 0;
|
if (page.locator("#Export_ThreeBillList_Button").count() > 0) {
|
||||||
if (exportBtnExists) {
|
|
||||||
System.out.println("[*] Clicking export button...");
|
System.out.println("[*] Clicking export button...");
|
||||||
Download[] downloadHolder = new Download[1];
|
// 设置下载目录
|
||||||
page.onDownload((java.util.function.Consumer<Download>) download -> {
|
Path downloadPath = Path.of("downloads").toAbsolutePath().normalize();
|
||||||
downloadHolder[0] = download;
|
java.nio.file.Files.createDirectories(downloadPath);
|
||||||
});
|
// 点击主导出按钮打开对话框,再用 JS click 触发对话框内导出按钮
|
||||||
page.locator("#Export_ThreeBillList_Button").first().click();
|
Download dl = page.waitForDownload(
|
||||||
if (downloadHolder[0] != null) {
|
new Page.WaitForDownloadOptions().setTimeout(300000),
|
||||||
System.out.println("[*] Waiting for download to complete...");
|
() -> {
|
||||||
Download dl = downloadHolder[0];
|
page.locator("#Export_ThreeBillList_Button").first().click();
|
||||||
Path downloadPath = Path.of("downloads");
|
sleep(2000);
|
||||||
java.nio.file.Files.createDirectories(downloadPath);
|
System.out.println("[*] Triggering dialog export via JS...");
|
||||||
Path savedFile = downloadPath.resolve(dl.suggestedFilename());
|
page.evaluate("document.querySelectorAll('button').forEach(b => { if (b.textContent.trim() === '导出') b.click(); })");
|
||||||
dl.saveAs(savedFile);
|
});
|
||||||
System.out.println("[+] Downloaded to: " + savedFile);
|
System.out.println("[*] Waiting for download to complete...");
|
||||||
if (java.nio.file.Files.size(savedFile) == 0) {
|
Path savedFile = downloadPath.resolve(dl.suggestedFilename());
|
||||||
System.out.println("[-] Downloaded file is empty");
|
dl.saveAs(savedFile);
|
||||||
} else {
|
System.out.println("[+] Download saved to: " + savedFile);
|
||||||
System.out.println("[+] Download size: " + java.nio.file.Files.size(savedFile) + " bytes");
|
if (java.nio.file.Files.size(savedFile) == 0) {
|
||||||
}
|
System.out.println("[-] Downloaded file is empty");
|
||||||
}
|
} else {
|
||||||
} else {
|
System.out.println("[+] Download size: " + java.nio.file.Files.size(savedFile) + " bytes");
|
||||||
System.out.println("[!] Export button not found");
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
screenshot(page, "after_export");
|
screenshot(page, "after_export");
|
||||||
System.out.println("[+] Query and export completed!");
|
System.out.println("[+] Query and export completed!");
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user