gpt4 book ai didi

java - 显式等待无法正常工作,即使加载了元素,仍然需要大量时间才能继续执行

转载 作者:行者123 更新时间:2023-11-30 03:05:22 25 4
gpt4 key购买 nike

我的代码

/**
* This method will explicit wait for an element to be visible.
* @param element
* element
* @param timeOut
* timeout in seconds
*/
@Override
public void explicitWaitForElement(String element, int timeOut) {
try {
LOGGER.debug("In explicitWaitForElement Finction");
String locator = getConstantValue(element);
LOGGER.debug("locator constant Value : " , locator);
setWait(getWebDriver(), timeOut);
getWait().until(
ExpectedConditions.visibilityOfElementLocated(By
.xpath(locator)));
} catch (Exception e) {
LOGGER.error(
"Within explicitWaitForElement - Element not found until timeout:",
e);
}
}

/**
* This method will explicit wait for an element to be visible.
*
* @param element
* element
* @param timeOut
* timeout in seconds
*/
@Override
public void explicitWaitForEleDisappers(String element, int timeOut) {
try {
LOGGER.debug("In explicitWaitForEleDisappers Finction");
String locator = getConstantValue(element);
LOGGER.debug("locator constant Value : " , locator);
setWait(getWebDriver(), timeOut);
getWait().until(
ExpectedConditions.invisibilityOfElementLocated(By
.xpath(locator)));
} catch (Exception e) {
LOGGER.error(
"Within explicitWaitForElement - Element not found until timeout:",
e);
}
}

Setter用于等待:

 /**
* Setter for wait.
*
* @param timeOut timeout
* @param wd webDriver
*/
public void setWait(WebDriver wd, int timeOut) {
this.wait = new WebDriverWait(wd, timeOut);
this.wait.ignoring(StaleElementReferenceException.class, WebDriverException.class);
}
  • 等待。
      • @return 等待值*/公共(public) WebDriverWait getWait() {返回等待;}

最佳答案

selenium中有WebDriverWait功能,可以设置显式等待。您正在使用 selenium webdriver,那么最好使用 WebDriverWait 来等待元素。按照下面的代码操作

protected WebElement waitForPresent(final String locator, long timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
WebElement ele = null;
try {
ele = wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
} catch (Exception e) {
throw e;
}
return ele;
}

protected WebElement waitForNotPresent(final String locator, long timeout) {
timeout = timeout * 1000;
long startTime = System.currentTimeMillis();
WebElement ele = null;
while ((System.currentTimeMillis() - startTime) < timeout) {
try {
ele = findElement(locator);
Thread.sleep(1000);
} catch (Exception e) {
break;
}
}
return ele;
}

关于java - 显式等待无法正常工作,即使加载了元素,仍然需要大量时间才能继续执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com