-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
goodluck-checkin-service/src/main/java/com/goodluck/checkin/utils/SeleniumUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |