Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-roy committed Nov 28, 2024
1 parent 12d2458 commit ccd7d8c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public class CheckinController {
@PostMapping("/buguTV")
@ApiOperation("buguTV")
public R<Boolean> buguTVCheckIn() {
buguTv.loginAndCheckin();
buguTv.loginAndCheckIn();
return R.success(Boolean.TRUE);
}

@PostMapping("/shaoshuren")
@ApiOperation("shaoshuren")
public R<Boolean> shaoshurenCheckIn() {
shaoshuren.loginAndCheckin();
shaoshuren.loginAndCheckIn();
return R.success(Boolean.TRUE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ public void loginAndCheckIn() {
log.info("--------------------布谷tv每日签到程序启动----------------------");
WebDriver browser = initBrowser();
try {
WebDriverWait wait = new WebDriverWait(browser, 20); //
WebDriverWait wait = new WebDriverWait(browser, 10); //
browser.get(LOGIN_URL);

// 点击登录按钮,触发登录弹窗
WebElement loginTriggerButton = wait.until(ExpectedConditions.elementToBeClickable(
By.cssSelector("a.login-btn.navbar-button")));
loginTriggerButton.click();

// File screenshot = ((TakesScreenshot) browser).getScreenshotAs(OutputType.FILE);
// Files.copy(screenshot.toPath(), Paths.get("screenshot.png"));

// 等待表单内容加载完成
WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("input[name='username']")));
Expand All @@ -82,6 +79,10 @@ public void loginAndCheckIn() {
usernameField.sendKeys(username); // 替换为实际的用户名或邮箱
passwordField.sendKeys(password); // 替换为实际的密码

// 截图
// File screenshot = ((TakesScreenshot) browser).getScreenshotAs(OutputType.FILE);
// Files.copy(screenshot.toPath(), Paths.get("screenshot.png"));

// 点击登录按钮
loginButton.click();

Expand All @@ -98,12 +99,12 @@ public void loginAndCheckIn() {
return;
}
log.info("布谷tv未签到,尝试签到...");
WebElement signInButton = new WebDriverWait(browser, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@class, 'go-user-qiandao')]")));
WebElement signInButton = new WebDriverWait(browser, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@class, 'go-user-qiandao')]")));
if (signInButton == null) {
throw new BusinessException("签到按钮未找到!");
}
signInButton.click();
alreadySignIn = safeWaitForElement(browser, By.xpath("//button[contains(text(), '今日已签到')]"), 10);
alreadySignIn = safeWaitForElement(browser, By.xpath("//button[contains(text(), '今日已签到')]"), 5);
if (alreadySignIn == null) {
throw new BusinessException("布谷tv签到失败!未找到签到成功标记!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.goodluck.checkin.site;


import com.goodluck.common.exception.BusinessException;
import com.goodluck.common.resp.R;
import com.google.common.base.Throwables;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,6 +21,8 @@

import java.time.Duration;

import static com.goodluck.checkin.utils.SeleniumUtils.safeWaitForElement;

/**
* @author liuleyi
*/
Expand Down Expand Up @@ -51,9 +55,9 @@ private static WebDriver initBrowser() {
return browser;
}

@Scheduled(cron = "0 0 7 * * ?")
public void loginAndCheckin() {
log.info("少数人开始登录并签到...");
@Scheduled(fixedDelay = 11 * 60 * 60 * 1000)
public void loginAndCheckIn() {
log.info("------------------少数人每日签到程序启动----------------");
WebDriver browser = initBrowser();
try {
// 打开登录页面
Expand All @@ -65,57 +69,55 @@ public void loginAndCheckin() {
// 找到邮箱输入框并输入邮箱
WebElement emailField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
Thread.sleep(1000);
emailField.sendKeys(username); // 替换为你的邮箱
emailField.sendKeys(username);

// 找到密码输入框并输入密码
WebElement passwordField = browser.findElement(By.id("password"));
Thread.sleep(1000);
passwordField.sendKeys(password); // 替换为你的密码
passwordField.sendKeys(password);

Thread.sleep(1000);
// 点击登录按钮
WebElement loginButton = browser.findElement(By.id("login_submit"));
loginButton.click();

log.info("少数人登录成功");
// 等待登录完成
wait.until(ExpectedConditions.urlContains("/user"));

// 定位签到按钮
WebElement signInButton = new WebDriverWait(browser, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("checkin")) // 按钮的 ID 为 "checkin"
);
if (signInButton == null) {
log.error("签到按钮未找到!");
// 等待签到成功提示
WebElement alreadySignIn = safeWaitForElement(browser, By.xpath("//a[contains(text(), '已签到')]"), 5);
if (alreadySignIn != null) {
log.info("今日已签到!提示信息: " + alreadySignIn.getText());
return;
}

// 检查按钮是否已禁用(通过 "disabled" 属性或类名)
if (signInButton == null || signInButton.getAttribute("class").contains("disabled") ||
signInButton.getAttribute("disabled") != null) {
log.info("今日已签到,无需重复操作。");
} else {
log.info("未签到,尝试签到...");
signInButton.click(); // 点击签到按钮

// 等待签到成功提示
WebElement successMessage = new WebDriverWait(browser, 10).until(
ExpectedConditions.presenceOfElementLocated(By.xpath("//p[contains(text(), '已签到')]"))
);
log.info("签到成功!提示信息: " + successMessage.getText());
// 定位签到按钮 // 按钮的 ID 为 "checkin"
WebElement signInButton = safeWaitForElement(browser, By.id("checkin"), 1);
if (signInButton == null) {
throw new BusinessException("签到按钮未找到!");
}
log.info("少数人未签到,尝试签到...");
// 点击签到按钮
signInButton.click();

alreadySignIn = safeWaitForElement(browser, By.xpath("//a[contains(text(), '已签到')]"), 5);
// 等待签到成功提示
if (alreadySignIn == null) {
throw new BusinessException("少数人签到成功按钮未找到,签到失败!");
}
log.info("少数人签到成功!提示信息: " + alreadySignIn.getText());
} catch (Exception e) {
log.error("签到过程中出现问题: " + e.getMessage());
log.error("少数人签到过程中出现问题: [{}]", Throwables.getStackTraceAsString(e));
} finally {

// 关闭浏览器
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
Thread.sleep(3000);
browser.quit();
} catch (Exception e) {
log.error("关闭浏览器失败 {}", Throwables.getStackTraceAsString(e));
}
browser.quit();

}
log.info("少数人结束登录并签到...");
log.info("--------------------少数人每日签到程序结束----------------------");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.goodluck.checkin.utils;

import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

@Slf4j
public class SeleniumUtils {

/**
* 等待元素出现,不抛出异常
* @param driver WebDriver 实例
* @param by 查找的条件
* @param timeout 超时时间(秒)
* @return 找到的元素;如果未找到,返回 null
*/
public static WebElement safeWaitForElement(WebDriver driver, By by, int timeout) {
try {
WebDriverWait wait = new WebDriverWait(driver, timeout);
return wait.until(driver1 -> {
List<WebElement> elements = driver1.findElements(by);
return elements.isEmpty() ? null : elements.get(0);
});
} catch (Exception e) {
log.info("safeWaitForElement byInfo {}, exception:{}", by.toString(), Throwables.getStackTraceAsString(e));
return null; // 未找到,返回 null
}
}
}

0 comments on commit ccd7d8c

Please sign in to comment.