修复了"学习通"平台中文档浏览任务的相关逻辑错误

This commit is contained in:
2025-04-08 10:02:21 +08:00
parent acc90b6bf3
commit 624f8723da
2 changed files with 36 additions and 8 deletions

View File

@@ -0,0 +1,7 @@
package work.slhaf.exception;
public class UnexpectedNumberTypeException extends RuntimeException {
public UnexpectedNumberTypeException(String message) {
super(message);
}
}

View File

@@ -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) {