From 624f8723da33029bb84de32aa074321dccfb6da6 Mon Sep 17 00:00:00 2001 From: slhaf Date: Tue, 8 Apr 2025 10:02:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86"=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E9=80=9A"=E5=B9=B3=E5=8F=B0=E4=B8=AD=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E4=BB=BB=E5=8A=A1=E7=9A=84=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnexpectedNumberTypeException.java | 7 ++++ .../java/work/slhaf/task/CxstudyTask.java | 37 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/main/java/work/slhaf/exception/UnexpectedNumberTypeException.java diff --git a/src/main/java/work/slhaf/exception/UnexpectedNumberTypeException.java b/src/main/java/work/slhaf/exception/UnexpectedNumberTypeException.java new file mode 100644 index 0000000..5475bb7 --- /dev/null +++ b/src/main/java/work/slhaf/exception/UnexpectedNumberTypeException.java @@ -0,0 +1,7 @@ +package work.slhaf.exception; + +public class UnexpectedNumberTypeException extends RuntimeException { + public UnexpectedNumberTypeException(String message) { + super(message); + } +} diff --git a/src/main/java/work/slhaf/task/CxstudyTask.java b/src/main/java/work/slhaf/task/CxstudyTask.java index dfcb6dc..d44158b 100644 --- a/src/main/java/work/slhaf/task/CxstudyTask.java +++ b/src/main/java/work/slhaf/task/CxstudyTask.java @@ -9,10 +9,7 @@ import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import work.slhaf.config.Config; -import work.slhaf.exception.AiRequestException; -import work.slhaf.exception.AiResponseFormatException; -import work.slhaf.exception.OcrRequestException; -import work.slhaf.exception.TargetClassNotFoundException; +import work.slhaf.exception.*; import work.slhaf.pojo.answer.Answer; import work.slhaf.pojo.answer.MultiChoiceAnswer; import work.slhaf.pojo.answer.SingleChoiceAnswer; @@ -111,14 +108,38 @@ public class CxstudyTask { try { driver.switchTo().frame(i).switchTo().frame(2); log.info("开始处理文档浏览..."); - double lastHeight = (double) executor.executeScript("return document.body.scrollHeight;"); - double currentHeight = 0; + Object lastHeightObject = executor.executeScript("return document.body.scrollHeight;"); + String numberType = lastHeightObject.getClass().getSimpleName(); + var lastHeight = switch (numberType){ + case "Long" -> (Long) lastHeightObject; + case "Integer" -> (Integer) lastHeightObject; + case "Double" -> (Double) lastHeightObject; + default -> throw new UnexpectedNumberTypeException("未设置处理策略的高度值类型:" + numberType); + }; + var currentHeight = switch (numberType){ + case "Long" -> 0L; + case "Integer" -> 0; + case "Double" -> 0.0; + default -> 0; + }; int step = 2000; while (currentHeight < lastHeight) { executor.executeScript("window.scrollBy(0, arguments[0]);", step); Thread.sleep(600); - currentHeight = (double) executor.executeScript("return document.body.scrollHeight;"); - lastHeight = (double) executor.executeScript("return document.body.scrollHeight;"); + Object tempLastHeightObject = executor.executeScript("return document.body.scrollHeight;"); + lastHeight = switch (numberType) { + case "Long" -> (Long) tempLastHeightObject; + case "Integer" -> (Integer) tempLastHeightObject; + case "Double" -> (Double) tempLastHeightObject; + default -> throw new UnexpectedNumberTypeException("未设置处理策略的高度值类型: " + numberType); + }; + Object tempCurrentHeightObject = executor.executeScript("return window.scrollY + window.innerHeight;"); + currentHeight = switch (numberType) { + case "Long" -> (Long) tempCurrentHeightObject; + case "Integer" -> (Integer) tempCurrentHeightObject; + case "Double" -> (Double) tempCurrentHeightObject; + default -> throw new UnexpectedNumberTypeException("未设置处理策略的高度值类型: " + numberType); + }; } log.info("文档浏览任务完成!"); } catch (WebDriverException ignored) {