【问题标题】:Selenium 3.4 how to use changed wait.untilSelenium 3.4 如何使用改变了wait.until
【发布时间】:2026-01-23 02:25:01
【问题描述】:

因此,对于 Selenium 3.4,我以前工作的 wait.untils 无法正常工作(已被新方法取代)。不过,我似乎无法让新方法发挥作用。

我正在使用

import com.google.common.base.Function;

旧代码:

public boolean waitForURLToMatch(String expectedURL, int waitTime){
    WebDriverWait wait = new WebDriverWait(driver, waitTime);
    wait.until(EcpectedConditions.urlMatches(expectedURL));
}

新代码:

public boolean waitForURLToMatch(String expectedURL, int waitTime){
    WebDriverWait wait = new WebDriverWait(driver, waitTime);
    wait.until(new Function<WebDriver, boolean>){

        @Override
        public boolean apply(WebDriver driver) {
            return driver.getCurrentUrl().equals(expectedURL);
        }
    }
}

新代码在eclipse中出现错误: Syntax error on tokens, InterfaceHeader expected instead

关于我哪里出错了有什么想法吗?

【问题讨论】:

  • urlMatches 将检查正则表达式,使用urlToBeurlContains。此外,如果您没有使用 guava 库的任何显式功能,则无需更新。并且您提到的错误已在最新版本中修复
  • @Madhan 感谢您的回复,我将如何使用 urlToBe 或 urlContains,until 方法已更改,谓词已删除,现在只有 until(Function是真的)。我不确定我提到的任何错误?
  • wait.until() 中使用ExpectedConditions
  • @Madhan until 在 3.2 中更改并且不再接受 ExpectedConditions,因此是这个问题的原因。
  • 这对我有用。有一个小细节。它指的是布尔对象,而不是布尔原语。

标签: java selenium selenium-webdriver selenium3


【解决方案1】:

所以经过大量谷歌搜索后,我最终发现问题只是语法。

这行得通:

public boolean waitForURLToMatch(String expectedURL, int waitTime){
    Wait<WebDriver> wait = new WebDriverWait(driver, waitTime);
    Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            String currentURL = driver.getCurrentUrl();
            if(currentURL.equals(expectedURL))
            {
                truefalse = true;
                return truefalse;
            }
            truefalse = false;
            return truefalse;
        }
    };
    try{
        wait.until(function);
    } catch (TimeoutException e){   
    }
    return truefalse;
}

编辑:好吧,看来这只是一个类路径冲突,现在一切正常,类路径冲突与 Selenium 一起删除了已弃用的 until(predicate) 混淆了这个问题。

【讨论】: