/**
* <一句话功能简述> selenium查找元素
* <功能详细描述>
* author: zhanggw
* 创建时间: 2022/4/24
* @param driver selenium驱动
* @param by 元素定位
* @param waitMaxSecond 最大等待时间,单位秒
* @return org.openqa.selenium.WebElement selenium元素
*/
public static WebElement findElementAndWait(WebDriver driver, By by, int waitMaxSecond) {
WebElement webElement = null;
try{
if(driver == null || by == null){
return null;
}
logger.debug("selenium开始寻找元素:{}", by.toString());
WebDriverWait webDriverWait = new WebDriverWait(driver, waitMaxSecond);
webDriverWait.until(ExpectedConditions.presenceOfElementLocated(by));
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by));
for(int i=0; i < 5; i++){
webElement = driver.findElement(by);
if(webElement.isEnabled() && webElement.isDisplayed()){
logger.debug("selenium找到元素:{}", by.toString());
return webElement;
}else{
logger.debug("selenium未找到元素:{},需等待{}ms", by.toString(), waitMaxSecond*1000/5);
Thread.sleep(waitMaxSecond*1000/5);
}
}
}catch (Exception e){
logger.debug("selenium未找到元素" + by.toString() + "异常!");
}
return webElement;
}
|