From ea0f566e1220f4788a86b9fa092488094fff5e36 Mon Sep 17 00:00:00 2001 From: Niko <1377382065@qq.com> Date: Tue, 5 May 2026 15:06:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=20=E2=80=94=20=E4=BD=BF=E7=94=A8=20waitForDo?= =?UTF-8?q?wnload=20=E5=B9=B6=E8=BE=93=E5=87=BA=E7=BB=9D=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用 page.waitForDownload 替代 onDownload + sleep,正确捕获下载事件 - 增加 5 分钟超时,等待服务器生成大文件 - 输出文件绝对路径和大小,不再找不到下载文件 - 只触发一次下载,不再重复 Co-Authored-By: Claude Opus 4.7 --- src/main/java/com/ets/scraper/EtsScraper.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/ets/scraper/EtsScraper.java b/src/main/java/com/ets/scraper/EtsScraper.java index f5aedf0..500cc61 100644 --- a/src/main/java/com/ets/scraper/EtsScraper.java +++ b/src/main/java/com/ets/scraper/EtsScraper.java @@ -142,33 +142,31 @@ public class EtsScraper { screenshot(page, "after_query"); - // 点击导出按钮 - boolean exportBtnExists = page.locator("#Export_ThreeBillList_Button").count() > 0; - if (exportBtnExists) { + // 点击导出按钮(先弹出导出选项对话框,再点击对话框内的导出) + if (page.locator("#Export_ThreeBillList_Button").count() > 0) { System.out.println("[*] Clicking export button..."); - Download[] downloadHolder = new Download[1]; - page.onDownload((java.util.function.Consumer) download -> { - downloadHolder[0] = download; - }); - page.locator("#Export_ThreeBillList_Button").first().click(); - if (downloadHolder[0] != null) { - System.out.println("[*] Waiting for download to complete..."); - Download dl = downloadHolder[0]; - Path downloadPath = Path.of("downloads"); - java.nio.file.Files.createDirectories(downloadPath); - Path savedFile = downloadPath.resolve(dl.suggestedFilename()); - dl.saveAs(savedFile); - System.out.println("[+] Downloaded to: " + savedFile); - if (java.nio.file.Files.size(savedFile) == 0) { - System.out.println("[-] Downloaded file is empty"); - } else { - System.out.println("[+] Download size: " + java.nio.file.Files.size(savedFile) + " bytes"); - } - } - } else { - System.out.println("[!] Export button not found"); - } - + // 设置下载目录 + Path downloadPath = Path.of("downloads").toAbsolutePath().normalize(); + java.nio.file.Files.createDirectories(downloadPath); + // 点击主导出按钮打开对话框,再用 JS click 触发对话框内导出按钮 + Download dl = page.waitForDownload( + new Page.WaitForDownloadOptions().setTimeout(300000), + () -> { + page.locator("#Export_ThreeBillList_Button").first().click(); + sleep(2000); + System.out.println("[*] Triggering dialog export via JS..."); + page.evaluate("document.querySelectorAll('button').forEach(b => { if (b.textContent.trim() === '导出') b.click(); })"); + }); + System.out.println("[*] Waiting for download to complete..."); + Path savedFile = downloadPath.resolve(dl.suggestedFilename()); + dl.saveAs(savedFile); + System.out.println("[+] Download saved to: " + savedFile); + if (java.nio.file.Files.size(savedFile) == 0) { + System.out.println("[-] Downloaded file is empty"); + } else { + System.out.println("[+] Download size: " + java.nio.file.Files.size(savedFile) + " bytes"); + } + } screenshot(page, "after_export"); System.out.println("[+] Query and export completed!");