李昭辉 1 month ago
parent
commit
7e2e2a2a2a
  1. 3
      .gitignore
  2. 512
      src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java
  3. 2
      src/main/resources/META-INF/MANIFEST.MF
  4. 3
      target/classes/META-INF/MANIFEST.MF
  5. 5
      target/maven-archiver/pom.properties

3
.gitignore

@ -22,5 +22,8 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
.vscode/
target/
target/classes/META-INF/MANIFEST.MF
.vscode/ .vscode/

512
src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java

@ -15,6 +15,8 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -34,6 +36,8 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import java.nio.file.Files;
/** /**
* Selenium 模拟登录并获取 Cookie 的示例 * Selenium 模拟登录并获取 Cookie 的示例
* 支持多种登录方式表单登录OAuthAJAX 登录等 * 支持多种登录方式表单登录OAuthAJAX 登录等
@ -93,7 +97,7 @@ public class SeleniumLoginWithCookieRecorder {
// 转换为 JSON // 转换为 JSON
String jsonParams = gson.toJson(params); String jsonParams = gson.toJson(params);
System.out.println("请求参数: " + jsonParams); // System.out.println("请求参数: " + jsonParams);
// 设置请求体 // 设置请求体
httpPost.setEntity(new StringEntity(jsonParams, "UTF-8")); httpPost.setEntity(new StringEntity(jsonParams, "UTF-8"));
@ -101,7 +105,7 @@ public class SeleniumLoginWithCookieRecorder {
// 发送请求 // 发送请求
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("API 响应: " + responseBody); // System.out.println("API 响应: " + responseBody);
// 解析响应 // 解析响应
return extractCaptchaFromResponse(responseBody); return extractCaptchaFromResponse(responseBody);
@ -138,7 +142,7 @@ public class SeleniumLoginWithCookieRecorder {
/** /**
* 初始化 WebDriver * 初始化 WebDriver
*/ */
public void setupDriver() { public void setupDriver(String proxy) {
// 自动下载和管理 ChromeDriver // 自动下载和管理 ChromeDriver
@ -152,6 +156,13 @@ public class SeleniumLoginWithCookieRecorder {
// Chrome 选项配置 // Chrome 选项配置
ChromeOptions options = new ChromeOptions(); ChromeOptions options = new ChromeOptions();
// 配置SOCKS5代理
if (proxy != null && !proxy.isEmpty()) {
String proxyConfig = "--proxy-server=" + proxy;
options.addArguments(proxyConfig);
System.out.println("已配置SOCKS5代理: " + proxyConfig);
}
// 可选:无头模式(不显示浏览器界面) // 可选:无头模式(不显示浏览器界面)
// options.addArguments("--headless"); // options.addArguments("--headless");
options.addArguments("--headless=new"); options.addArguments("--headless=new");
@ -205,6 +216,9 @@ public class SeleniumLoginWithCookieRecorder {
// [1, 2, 3, 4, 5]})"); // [1, 2, 3, 4, 5]})");
// driver.executeScript("Object.defineProperty(navigator, 'languages', {get: () // driver.executeScript("Object.defineProperty(navigator, 'languages', {get: ()
// => ['zh-CN', 'zh', 'en']})"); // => ['zh-CN', 'zh', 'en']})");
// 设置超时时间
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
wait = new WebDriverWait(driver, Duration.ofSeconds(15)); wait = new WebDriverWait(driver, Duration.ofSeconds(15));
System.out.println("WebDriver 初始化完成"); System.out.println("WebDriver 初始化完成");
@ -213,15 +227,31 @@ public class SeleniumLoginWithCookieRecorder {
/** /**
* 通用表单登录 * 通用表单登录
*/ */
public void loginToCrossCheck(String username, String password, String filePath) { public boolean loginToCrossCheck(String username, String password, String filePath, String tenant,
String capture_path) {
try { try {
System.out.println("开始 Crossref 登录流程..."); System.out.println("开始 Crossref 登录流程...");
// 打开登录页面 // 打开登录页面
driver.get("https://crossref-26027.turnitin.com/originality/inbox/713daa1b-0685-4cc7-89ec-6b6c7cbc0411"); // driver.get("https://crossref-26027.turnitin.com/originality/inbox/713daa1b-0685-4cc7-89ec-6b6c7cbc0411");
// driver.get("https://" + tenant + ".turnitin.com/home/sign-in");
// 增加页面加载等待时间 // 增加页面加载等待时间
Thread.sleep(3000); // 等待5秒确保页面完全加载 // Thread.sleep(3000); // 等待5秒确保页面完全加载
try {
// 增加超时设置
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
driver.get("https://" + tenant + ".turnitin.com/home/sign-in");
// 等待页面元素出现而不是固定等待
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
} catch (TimeoutException e) {
// 记录日志并尝试刷新页面
System.err.println("页面加载超时,尝试刷新...");
driver.navigate().refresh();
// 再次等待元素出现
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
}
// 调试:打印页面标题和URL // 调试:打印页面标题和URL
System.out.println("当前页面标题: " + driver.getTitle()); System.out.println("当前页面标题: " + driver.getTitle());
@ -265,64 +295,24 @@ public class SeleniumLoginWithCookieRecorder {
if (usernameField == null) { if (usernameField == null) {
System.err.println("无法定位到用户名输入框,页面可能没有正确加载"); System.err.println("无法定位到用户名输入框,页面可能没有正确加载");
// captureScreenshot("element_not_found"); // captureScreenshot("element_not_found");
return; return false;
} }
// 输入用户名 // 输入用户名
usernameField.clear(); usernameField.clear();
usernameField.sendKeys(username); usernameField.sendKeys(username);
System.out.println("已输入用户名"); System.out.println("已输入用户名: " + username);
// 输入密码 // 输入密码
WebElement passwordField = extendedWait WebElement passwordField = extendedWait
.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))); .until(ExpectedConditions.visibilityOfElementLocated(By.id("password")));
passwordField.clear(); passwordField.clear();
passwordField.sendKeys(password); passwordField.sendKeys(password);
System.out.println("已输入密码"); System.out.println("已输入密码: " + password);
Thread.sleep(3000); Thread.sleep(3000);
// 验证码处理 // 验证码处理
String cap_first = "0"; // String cap_first = "0";
try { for (int i = 0; i < 3; i++) {
WebElement captchaImage = extendedWait
.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("img[alt='Captcha image']")));
String src1 = captchaImage.getAttribute("src");
if (src1 != null && !src1.isEmpty()) {
src1 = src1.replace("data:image/png;base64,", "").replace("\n", "").trim();
System.out.println("验证码图片URL: " + src1);
String result_cap = this.recognizeCaptcha(src1 + "YII=");
System.out.println("验证码识别结果: " + result_cap);
if (result_cap != null && !result_cap.trim().isEmpty()) {
result_cap = result_cap.replace(" ", "");
WebElement captchaField = extendedWait
.until(ExpectedConditions.visibilityOfElementLocated(By.id("captcha-input")));
captchaField.clear();
captchaField.sendKeys(result_cap);
System.out.println("已输入验证码");
// captureScreenshot("captcha_done");
Thread.sleep(3000);
}
}
} catch (Exception e) {
System.out.println("未找到验证码或验证码处理失败: " + e.getMessage());
cap_first = "1";
}
// 点击登录按钮
WebElement signinButton = extendedWait.until(
ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-testid='sign-in-btn']")));
signinButton.click();
System.out.println("已点击登录按钮");
boolean isLoggedIn = isLoggedIn();
// 处理验证码
if (cap_first.equals("1") && !isLoggedIn) {
Thread.sleep(3000);
// 验证码处理
try { try {
WebElement captchaImage = extendedWait WebElement captchaImage = extendedWait
.until(ExpectedConditions .until(ExpectedConditions
@ -331,60 +321,103 @@ public class SeleniumLoginWithCookieRecorder {
if (src1 != null && !src1.isEmpty()) { if (src1 != null && !src1.isEmpty()) {
src1 = src1.replace("data:image/png;base64,", "").replace("\n", "").trim(); src1 = src1.replace("data:image/png;base64,", "").replace("\n", "").trim();
System.out.println("验证码图片URL2: " + src1); // System.out.println("验证码图片URL: " + src1);
String result_cap = this.recognizeCaptcha(src1 + "YII="); String result_cap = this.recognizeCaptcha(src1 + "YII=");
System.out.println("验证码识别结果2: " + result_cap); System.out.println("验证码识别结果" + (i + 1) + ": " + result_cap);
if (result_cap != null && !result_cap.trim().isEmpty()) { if (result_cap != null && !result_cap.trim().isEmpty()) {
result_cap = result_cap.replace(" ", ""); result_cap = result_cap.replace(" ", "");
WebElement captchaField = extendedWait WebElement captchaField = extendedWait
.until(ExpectedConditions.visibilityOfElementLocated(By.id("captcha-input"))); .until(ExpectedConditions.visibilityOfElementLocated(By.id("captcha-input")));
captchaField.clear(); captchaField.clear();
captchaField.sendKeys(result_cap); captchaField.sendKeys(result_cap);
System.out.println("已输入验证码2"); System.out.println("已输入验证码" + (i + 1) + ": " + result_cap);
// captureScreenshot("captcha_done"); if (!capture_path.isEmpty()) {
captureScreenshot("captcha_write_done" + (i + 1), capture_path);
}
Thread.sleep(3000); Thread.sleep(3000);
} }
// 点击登录按钮
WebElement signinButton2 = extendedWait.until(
ExpectedConditions
.elementToBeClickable(By.cssSelector("button[data-testid='sign-in-btn']")));
signinButton2.click();
System.out.println("已点击登录按钮2");
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("未找到验证码或验证码处理失败2: " + e.getMessage()); System.out.println("未找到验证码或验证码处理失败" + (i + 1) + ": " + e.getMessage());
// cap_first = "1";
}
// 点击登录按钮
WebElement signinButton = extendedWait.until(
ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-testid='sign-in-btn']")));
signinButton.click();
Thread.sleep(13000);
System.out.println("已点击登录按钮" + (i + 1));
if (!capture_path.isEmpty()) {
captureScreenshot("login_button_click" + (i + 1), capture_path);
} }
boolean isLoggedIn = isLoggedIn();
if (isLoggedIn) {
// System.out.println("登录成功");
break;
} else {
try {
// 获取错误提示文本
WebElement boldText = driver.findElement(By.cssSelector("wi-banner.error b"));
String errorText = boldText.getText();
System.out.println("登录错误提示" + (i + 1) + ": " + errorText);
// 如果错误提示包含"Invalid username or password",则继续循环
if (errorText.contains("Too many attempts")) {
System.out.println("登录次数太多");
break;
}
} catch (Exception e) {
System.out.println("未找到登录错误提示处理失败" + (i + 1) + ": " + e.getMessage());
// cap_first = "1";
}
}
Thread.sleep(15000);
} }
// 等待登录结果 // 等待登录结果
Thread.sleep(3000); Thread.sleep(5000);
// 检查登录是否成功 // 检查登录是否成功
try { try {
// extendedWait.until(ExpectedConditions.urlContains("crossref-26027.turnitin.com/originality/inbox/"));
// System.out.println("登录成功,当前URL: " + driver.getCurrentUrl());
// 获取并保存 Cookie // 获取并保存 Cookie
isLoggedIn = isLoggedIn(); boolean isLoggedIn = isLoggedIn();
if (isLoggedIn) { if (isLoggedIn) {
saveNPCookiesToFile("crossref", filePath); saveNPCookiesToFile("crossref", filePath);
System.out.println("登录成功"); System.out.println("登录成功");
// 清空cookie
driver.manage().deleteAllCookies();
return true;
} else { } else {
System.out.println("登录失败"); System.out.println("登录失败");
// // 获取错误提示文本
WebElement boldText2 = driver.findElement(By.cssSelector("wi-banner.error b"));
String errorText2 = boldText2.getText();
System.out.println("登录错误提示: " + errorText2);
if (!capture_path.isEmpty()) {
captureScreenshot("login_error", capture_path);
}
// 清空cookie
driver.manage().deleteAllCookies();
return false;
} }
} catch (Exception e) { } catch (Exception e) {
System.err.println("登录可能失败,当前URL: " + driver.getCurrentUrl()); System.err.println("登录可能失败,当前URL: " + driver.getCurrentUrl());
// captureScreenshot("login_possible_failure"); // captureScreenshot("login_possible_failure");
return false;
} }
} catch (Exception e) { } catch (Exception e) {
// 清空cookie
driver.manage().deleteAllCookies();
System.err.println("登录过程中发生错误: " + e.getMessage()); System.err.println("登录过程中发生错误: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
// captureScreenshot("login_error"); // captureScreenshot("login_error");
return false;
} }
} }
@ -539,46 +572,161 @@ public class SeleniumLoginWithCookieRecorder {
} }
} }
/** public boolean loadCookiesAndAccessSite(String cookieFile, String tenant, String capture_path) {
* 从文件加载 Cookie 并设置到浏览器
*/
public void loadCookiesFromFile(String filename) {
try { try {
CookieData cookieData = objectMapper.readValue(new File(filename), CookieData.class); try {
// 增加超时设置
// 先导航到 Cookie 的域名 driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
driver.get(cookieData.getUrl()); // String targetUrl = "https://" + tenant + ".turnitin.com/home/";
driver.get("https://" + tenant + ".turnitin.com/home/sign-in");
// 等待页面元素出现而不是固定等待
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
} catch (TimeoutException e) {
// 记录日志并尝试刷新页面
System.err.println("保活页面加载超时,尝试刷新...");
driver.navigate().refresh();
// 再次等待元素出现
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
}
// 先导航到目标网站
// driver.get(targetUrl);
// System.out.println("访问保活Url " + targetUrl);
// 读取 Cookie 文件
List<String> lines = Files.readAllLines(Paths.get(cookieFile), StandardCharsets.UTF_8);
System.out.println("读取 Cookie 文件 " + cookieFile);
// 打印lines
// System.out.println(lines);
// 清空现有 Cookie // 清空现有 Cookie
driver.manage().deleteAllCookies(); driver.manage().deleteAllCookies();
System.out.println("清空现有 Cookie");
// 添加 Cookie // 解析并添加 Cookie
for (Cookie cookie : cookieData.getCookies()) { for (String line : lines) {
try { line = line.trim();
driver.manage().addCookie(cookie); if (line.isEmpty() || line.startsWith("#")) {
System.out.println("已添加 Cookie: " + cookie.getName()); continue;
} catch (Exception e) { }
System.err.println("添加 Cookie 失败 (" + cookie.getName() + "): " + e.getMessage());
String[] parts = line.split("\t");
if (parts.length >= 6) {
try {
String domain = parts[0].startsWith(".") ? parts[0].substring(1) : parts[0];
String name = parts[5];
String value = parts.length > 6 ? parts[6] : "";
// 移除值中的引号
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
// 创建并添加 Cookie
Cookie cookie = new Cookie.Builder(name, value)
.domain(domain)
.path(parts[2])
.isSecure("TRUE".equalsIgnoreCase(parts[3]))
.build();
driver.manage().addCookie(cookie);
System.out.println("已添加 Cookie: " + name);
} catch (Exception e) {
// 简单忽略解析错误的行
}
} }
} }
// 刷新页面使 Cookie 生效 // 刷新页面使 Cookie 生效
driver.navigate().refresh(); driver.navigate().refresh();
System.out.println("Cookie 加载完成,页面已刷新"); // 在当前cookie访问其他页面
// driver.get("https://crossref-26027.turnitin.com/home/");
try {
// 增加超时设置
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
driver.get("https://" + tenant + ".turnitin.com/home/");
// 等待页面元素出现而不是固定等待
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1[data-test-id='page-title']")));
} catch (TimeoutException e) {
// 记录日志并尝试刷新页面
System.err.println("页面加载超时,尝试刷新...");
driver.navigate().refresh();
// 再次等待元素出现
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1[data-test-id='page-title']")));
}
} catch (IOException e) { System.out.println("保活Cookie 加载完成,页面已刷新");
System.err.println("加载 Cookie 文件失败: " + e.getMessage()); Thread.sleep(5000);
if (!capture_path.isEmpty()) {
captureScreenshot("login_refresh", capture_path);
}
// 再刷新一次页面,确保cookie生效
driver.navigate().refresh();
Thread.sleep(5000);
// 使用CSS选择器
WebElement element = driver.findElement(By.cssSelector("h1[data-test-id='page-title']"));
String text = element.getText();
// 随机点击次数1~3下,确保元素可见
int clickCount = (int) (Math.random() * 3) + 1;
for (int i = 0; i < clickCount; i++) {
element.click();
}
// 随机获取一个元素点击,不包括链接元素,按钮元素。
try {
List<WebElement> elements = driver.findElements(By.cssSelector("*:not(a):not(button)"));
System.out.println("页面元素数量: " + elements.size());
if (!elements.isEmpty()) {
int clickElementCount = (int) (Math.random() * 2) + 1;
for (int i = 0; i < clickElementCount; i++) {
int randomIndex = (int) (Math.random() * elements.size());
WebElement randomElement = elements.get(randomIndex);
randomElement.click();
}
}
} catch (Exception e) {
System.out.println("随机元素不存在");
}
System.out.println("获取的文字是: " + text);
// 包含指定文本
if (text.contains("Welcome")) {
System.out.println("使用cookie刷新成功");
saveNPCookiesToFile("crossref", cookieFile);
// 清空cookie
driver.manage().deleteAllCookies();
return true;
} else {
System.out.println("使用cookie刷新失败");
// 清空cookie
driver.manage().deleteAllCookies();
return false;
}
} catch (Exception e) {
System.err.println("Cookie 刷新失败: " + e.getMessage());
// 清空cookie
driver.manage().deleteAllCookies();
return false;
} }
// return false;
} }
/** /**
* 截图保存 * 截图保存
*/ */
private void captureScreenshot(String filename) { private void captureScreenshot(String filename, String capture_path) {
try { try {
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File dest = new File("screenshot_" + filename + "_" + timestamp + ".png"); File dest = new File(capture_path + "screenshot_" + filename + "_" + timestamp + ".png");
com.google.common.io.Files.copy(screenshot, dest); com.google.common.io.Files.copy(screenshot, dest);
System.out.println("截图已保存: " + dest.getAbsolutePath()); System.out.println("截图已保存: " + dest.getAbsolutePath());
} catch (Exception e) { } catch (Exception e) {
@ -621,29 +769,183 @@ public class SeleniumLoginWithCookieRecorder {
SeleniumLoginWithCookieRecorder recorder = new SeleniumLoginWithCookieRecorder(); SeleniumLoginWithCookieRecorder recorder = new SeleniumLoginWithCookieRecorder();
try { try {
// 1. 初始化驱动 String user_name = "";
recorder.setupDriver(); String password = "";
if (args.length != 3) { String filePath = "";
System.err.println("请提供用户名、密码和文件路径作为参数"); String proxy = "";
String tenant = "crossref-26027";
String help = "0";
String capture_path = "";
String only_keep_cookie = "0";
String only_login = "0";
String json_accounts = "";
String waitTime = "";
ArrayList arr_accounts = new ArrayList<>();
// 传的参数增加参数名
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-u")) {
user_name = args[i + 1];
} else if (args[i].equals("-p")) {
password = args[i + 1];
} else if (args[i].equals("-f")) {
filePath = args[i + 1];
} else if (args[i].equals("-proxy")) {
proxy = args[i + 1];
} else if (args[i].equals("-t")) {
tenant = args[i + 1];
} else if (args[i].equals("-json_accounts")) {
json_accounts = args[i + 1];
arr_accounts = new Gson().fromJson(json_accounts, ArrayList.class);
} else if (args[i].equals("-help")) {
help = "1";
} else if (args[i].equals("-only_keep_cookie")) {
only_keep_cookie = "1";
} else if (args[i].equals("-only_login")) {
only_login = "1";
} else if (args[i].equals("-capture_path")) {
capture_path = args[i + 1];
// capture_path 以-开头或为空,默认当前目录
if (capture_path.startsWith("-") || capture_path.isEmpty()) {
capture_path = "./";
}
// 如果 capture_path 不以/结尾,添加/
if (!capture_path.endsWith("/")) {
capture_path += "/";
}
}
}
if (help.equals("1")) {
System.out.println("参数说明:");
System.out.println("-u '用户名',无-json_accounts时必填");
System.out.println("-p '密码',无-json_accounts时必填,-only_keep_cookie时可不填");
System.out.println("-f '保存 Cookie 的文件路径及文件名',无-json_accounts时必填");
System.out.println("-t '租户 ID'(可选,默认crossref-26027)");
System.out.println(
"-json_accounts '多个账号json格式:[{\"username\":\"\",\"password\":\"\",\"tenant\":\"\",\"filePath\":\"\",\"waitTime\":\"\"},...]',选填,可替代-u/-p/-t/-f参数");
System.out.println("-proxy '代理配置字串'(可选,例如 'socks5://host:port') ");
System.out.println("-capture_path '截图保存路径'(可选,没有此参数,则不截图)");
System.out.println("-only_keep_cookie: 只做cookie保活,不登录");
System.out.println("-only_login: 只登录,不做cookie保活");
System.out.println("-help: 显示帮助信息");
return; return;
} }
if (args[0].isEmpty() || args[1].isEmpty() || args[2].isEmpty()) {
System.err.println("用户名、密码或文件路径不能为空"); if ((user_name.isEmpty() || (password.isEmpty() && !only_keep_cookie.equals("1")) || filePath.isEmpty())
&& arr_accounts.isEmpty()) {
System.err.println("-help 查看帮助信息。用户名、密码、文件路径不能为空或-json_accounts参数不能为空。");
return; return;
} }
String user_name = args[0];
String password = args[1];
String filePath = args[2];
// 2. Crossref 登录示例(需要替换为真实账号) // 将-u/-p/-t/-f参数添加到arr_accounts中
recorder.loginToCrossCheck(user_name, password, filePath); if (arr_accounts.isEmpty()) {
Map<String, Object> accountMap = new HashMap<>();
accountMap.put("username", user_name);
accountMap.put("password", password);
accountMap.put("tenant", tenant);
accountMap.put("filePath", filePath);
accountMap.put("waitTime", "1");
arr_accounts.add(accountMap);
}
// 检查元素是否有username、password、tenant、filePath
for (Object obj : arr_accounts) {
// 检查元素是否为 Map 类型
if (obj instanceof Map) {
Map<String, Object> accountMap = (Map<String, Object>) obj;
user_name = (String) accountMap.get("username");
password = (String) accountMap.get("password");
tenant = (String) accountMap.get("tenant");
filePath = (String) accountMap.get("filePath");
// 检测是否有username、password、tenant、filePath,并且不能为空,为空则删除
if (user_name.isEmpty() || tenant.isEmpty() || filePath.isEmpty()) {
arr_accounts.remove(accountMap);
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("<res>" + currentTime + "|" + user_name
+ "|保活失败|数组元素删除,因为username、password、tenant、filePath不能为空</res>");
}
} else {
System.out.println("数组元素不是 Map 类型");
}
}
// 1. 初始化驱动(带代理配置)
recorder.setupDriver(proxy);
if (only_login.equals("1")) {
System.out.println("-only_login: 只登录,不做cookie保活");
} else {
// 遍历数组中的每个元素,保活cookie
for (Object obj : arr_accounts) {
// 检查元素是否为 Map 类型
Map<String, Object> accountMap = (Map<String, Object>) obj;
user_name = (String) accountMap.get("username");
password = (String) accountMap.get("password");
tenant = (String) accountMap.get("tenant");
filePath = (String) accountMap.get("filePath");
waitTime = String.valueOf(accountMap.get("waitTime"));
// 如果文件存在,加载文件中的 Cookie
if (Files.exists(Paths.get(filePath))) {
System.out.println("文件存在,加载文件中的 Cookie:" + filePath);
boolean testLoggedIn = recorder.loadCookiesAndAccessSite(filePath, tenant, capture_path);
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
if (testLoggedIn) {
System.out.println("<res>" + currentTime + "|" + user_name + "|cookie保活成功</res>");
// return;
} else {
System.out.println("<res>" + currentTime + "|" + user_name + "|cookie保活失败</res>");
}
}
// 等待间隔
if (waitTime != null && !waitTime.isEmpty()) {
try {
System.out.println("等待 " + waitTime + " 秒后继续下一个账号");
Thread.sleep((int) Double.parseDouble(waitTime) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
if (only_keep_cookie.equals("1")) {
System.out.println("-only_keep_cookie: 只做cookie保活,不登录");
return;
}
// 3. 验证登录状态 // 遍历数组中的每个元素,登录并保存cookie
boolean isLoggedIn = recorder.isLoggedIn(); for (Object obj : arr_accounts) {
System.out.println("登录状态: " + (isLoggedIn ? "成功" : "失败")); // 检查元素是否为 Map 类型
Map<String, Object> accountMap = (Map<String, Object>) obj;
user_name = (String) accountMap.get("username");
password = (String) accountMap.get("password");
tenant = (String) accountMap.get("tenant");
filePath = (String) accountMap.get("filePath");
waitTime = String.valueOf(accountMap.get("waitTime"));
// 2. Crossref 登录示例(需要替换为真实账号)
boolean isLoggedIn = recorder.loginToCrossCheck(user_name, password, filePath, tenant, capture_path);
// 3. 验证登录状态
// boolean isLoggedIn = recorder.isLoggedIn();
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(
"<res>" + currentTime + "|" + user_name + "|登录" + (isLoggedIn ? "成功" : "失败") + "</res>");
// 4. 可以在这里使用获取的 Cookie 进行后续操作 // 清空cookie
// recorder.loadCookiesFromFile("github_cookies_20231215_143022.json"); recorder.driver.manage().deleteAllCookies();
// 等待间隔
if (waitTime != null && !waitTime.isEmpty()) {
try {
System.out.println("等待 " + waitTime + " 秒后继续下一个账号");
Thread.sleep((int) Double.parseDouble(waitTime) * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("程序执行异常: " + e.getMessage()); System.err.println("程序执行异常: " + e.getMessage());

2
src/main/resources/META-INF/MANIFEST.MF

@ -1,3 +1,3 @@
Manifest-Version: 1.0 Manifest-Version: 1.0
Main-Class: com.seleniumLib.crossrefDownLib Main-Class: com.seleniumLib.SeleniumLoginWithCookieRecorder

3
target/classes/META-INF/MANIFEST.MF

@ -1,3 +0,0 @@
Manifest-Version: 1.0
Main-Class: com.seleniumLib.crossrefDownLib

5
target/maven-archiver/pom.properties

@ -1,5 +0,0 @@
#Generated by Maven
#Tue Dec 23 18:30:25 CST 2025
version=1.0-SNAPSHOT
groupId=com.example
artifactId=crossref
Loading…
Cancel
Save