Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-roy committed Nov 24, 2024
1 parent fb38b88 commit 3511ac8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goodluck.checkin.site;


import com.goodluck.common.exception.BusinessException;
import com.google.common.base.Throwables;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -76,29 +77,33 @@ public void loginAndCheckIn() {
browser.get(USER_URL);

// 检查是否签到
WebElement successMessage = safeWaitForElement(browser, By.xpath("//button[contains(text(), '今日已签到')]"), 5);
if (successMessage != null) {
log.info("布谷tv今日已签到!提示信息: " + successMessage.getText());
WebElement alreadySignIn = safeWaitForElement(browser, By.xpath("//button[contains(text(), '今日已签到')]"), 5);
if (alreadySignIn != null) {
log.info("布谷tv今日已签到!提示信息: " + alreadySignIn.getText());
return;
}
WebElement button = new WebDriverWait(browser, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@class, 'go-user-qiandao')]")));
log.info("布谷tv未签到,尝试签到...");
button.click();
button = safeWaitForElement(browser, By.xpath("//button[contains(text(), '今日已签到')]"), 5);
if (button == null) {
log.info("布谷tv签到失败!未找到签到成功标记" );
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(), '今日已签到')]"), 5);
if (alreadySignIn == null) {
throw new BusinessException("布谷tv签到失败!未找到签到成功标记!");
}
log.info("布谷tv签到成功");
} catch (Exception e) {
log.error("签到过程中出现问题: {} ", Throwables.getStackTraceAsString(e));
log.error("签到过程中出现问题: [{}] ", Throwables.getStackTraceAsString(e));
} finally {
// 关闭浏览器
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
browser.quit();
} catch (Exception e) {
log.error("关闭浏览器失败 {}", Throwables.getStackTraceAsString(e));
}
browser.quit();

}
log.info("--------------------布谷tv每日签到程序结束----------------------");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,11 @@ public void loginAndCheckIn() {
log.info("今日已签到!提示信息: " + alreadySignIn.getText());
return;
}

// 定位签到按钮 // 按钮的 ID 为 "checkin"
WebElement signInButton = safeWaitForElement(browser, By.id("checkin"), 1);

if (signInButton == null) {
log.error("签到按钮未找到!");
throw new BusinessException("签到按钮未找到!");
}

log.info("少数人未签到,尝试签到...");
// 点击签到按钮
signInButton.click();
Expand All @@ -112,10 +108,11 @@ public void loginAndCheckIn() {
// 关闭浏览器
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
browser.quit();
} catch (Exception e) {
log.error("关闭浏览器失败 {}", Throwables.getStackTraceAsString(e));
}
browser.quit();

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

0 comments on commit 3511ac8

Please sign in to comment.