diff --git a/src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java b/src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java index 171e157..9d9bdc0 100644 --- a/src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java +++ b/src/main/java/com/seleniumLib/SeleniumLoginWithCookieRecorder.java @@ -483,6 +483,12 @@ public class SeleniumLoginWithCookieRecorder { if (isLoggedIn) { saveNPCookiesToFile("crossref", filePath); System.out.println("登录成功"); + // 每隔五分钟刷新一下页面,保持登录状态 + // for (int i = 0; i < 12; i++) { + // Thread.sleep(300000); // 5分钟 + // driver.navigate().refresh(); + // System.out.println("已刷新页面"); + // } } else { System.out.println("登录失败"); // // 获取错误提示文本 @@ -693,16 +699,36 @@ public class SeleniumLoginWithCookieRecorder { } } - public void loadCookiesAndAccessSite(String cookieFile, String targetUrl, String capture_path) { + public boolean loadCookiesAndAccessSite(String cookieFile, String tenant, String capture_path) { try { + try { + // 增加超时设置 + driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60)); + // 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); - + // driver.get(targetUrl); + // System.out.println("访问保活Url " + targetUrl); // 读取 Cookie 文件 List lines = Files.readAllLines(Paths.get(cookieFile), StandardCharsets.UTF_8); + System.out.println("读取 Cookie 文件 " + cookieFile); + // 打印lines + // System.out.println(lines); // 清空现有 Cookie driver.manage().deleteAllCookies(); + System.out.println("清空现有 Cookie"); // 解析并添加 Cookie for (String line : lines) { @@ -741,14 +767,55 @@ public class SeleniumLoginWithCookieRecorder { // 刷新页面使 Cookie 生效 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']"))); + } + + System.out.println("保活Cookie 加载完成,页面已刷新"); 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(); + element.click(); + element.click(); + System.out.println("获取的文字是: " + text); + // 包含指定文本 + if (text.contains("Welcome")) { + System.out.println("使用cookie刷新成功"); + saveNPCookiesToFile("crossref", cookieFile); + return true; + } else { + System.out.println("使用cookie刷新失败"); + return false; + } } catch (Exception e) { - System.err.println("处理 Cookie 文件失败: " + e.getMessage()); + System.err.println("Cookie 刷新失败: " + e.getMessage()); + return false; } + // return false; } /** @@ -808,6 +875,8 @@ public class SeleniumLoginWithCookieRecorder { String tenant = "crossref-26027"; String help = "0"; String capture_path = ""; + String only_keep_cookie = "0"; + String only_login = "0"; // 传的参数增加参数名 for (int i = 0; i < args.length; i++) { @@ -823,6 +892,10 @@ public class SeleniumLoginWithCookieRecorder { tenant = args[i + 1]; } 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 以-开头或为空,默认当前目录 @@ -840,28 +913,44 @@ public class SeleniumLoginWithCookieRecorder { System.out.println("参数说明:"); System.out.println("-u '用户名',必填"); System.out.println("-p '密码',必填"); - System.out.println("-f 保存 Cookie 的文件路径及文件名,必填"); - System.out.println("-t 租户 ID(可选,默认crossref-26027)"); - System.out.println("-proxy 代理配置,可选(例如 'socks5://host:port')"); - System.out.println("-capture_path '截图保存路径'(没有此参数,则不截图;有参数不设置路径,默认当前目录'./')"); + System.out.println("-f '保存 Cookie 的文件路径及文件名',必填"); + System.out.println("-t '租户 ID'(可选,默认crossref-26027)"); + 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; } if (user_name.isEmpty() || password.isEmpty() || filePath.isEmpty()) { - System.err.println("用户名、密码、文件路径不能为空。-help 查看帮助信息1111"); + System.err.println("用户名、密码、文件路径不能为空。-help 查看帮助信息"); return; } // 1. 初始化驱动(带代理配置) recorder.setupDriver(proxy); - // 如果文件存在,加载文件中的 Cookie - // if (Files.exists(Paths.get(filePath))) { - // System.out.println("文件存在,加载文件中的 Cookie:" + filePath); - // String loginUrl = "https://" + tenant + ".turnitin.com/home"; - // recorder.loadCookiesAndAccessSite(filePath, loginUrl, capture_path); - // } + if (only_login.equals("1")) { + System.out.println("-only_login: 只登录,不做cookie保活"); + } else { + // 如果文件存在,加载文件中的 Cookie + if (Files.exists(Paths.get(filePath))) { + System.out.println("文件存在,加载文件中的 Cookie:" + filePath); + boolean testLoggedIn = recorder.loadCookiesAndAccessSite(filePath, tenant, capture_path); + if (testLoggedIn) { + System.out.println("cookie保活成功"); + return; + } else { + System.out.println("cookie保活失败"); + } + } + } + + if (only_keep_cookie.equals("1")) { + System.out.println("-only_keep_cookie: 只做cookie保活,不登录"); + return; + } // System.exit(0); // return; // 2. Crossref 登录示例(需要替换为真实账号)