【问题标题】:The method until(Function) in the type Wait is not applicable for the arguments (new Function(){})Wait 类型中的方法 until(Function) 不适用于参数 (new Function(){})
【发布时间】:2017-11-30 09:54:02
【问题描述】:

在 Selenium WebDriver 中编译我的代码时出现此错误:

@BeforeClass setUp java.lang.Error: 未解决的编译问题: Wait 类型中的 until(Function) 方法不适用于 arguments (new Function(){}) 函数无法解析为类型

我的代码是:

    Wait wait = new FluentWait(driver)    
            .withTimeout(30, SECONDS)    
            .pollingEvery(5, SECONDS)   
            .ignoring(NoSuchElementException.class);

    WebElement myLoginButton = wait.until(
        new Function() {    
            public WebElement apply(WebDriver driver) {    
                return driver.findElement(By.id("btnLogin"));    
            }
        }
    )

【问题讨论】:

  • 好的,因此查看您拥有的代码有点难以确定您要实现的目标。是否还有更多与此区块相关的代码没有共享new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("btnLogin")); }

标签: java selenium-webdriver


【解决方案1】:

为了创建你自己的函数,你应该提供Function<? super WebDriver, ?>函数。

或者,创建新对象ExpectedCondtions

例子:

Wait wait = new FluentWait(driver)    
            .withTimeout(30, SECONDS)    
            .pollingEvery(5, SECONDS)   
            .ignoring(NoSuchElementException.class);

WebElement myLoginButton = wait.until(
    new ExpectedCondition<WebElement>() {    

            public WebElement apply(WebDriver driver) {    
                return driver.findElement(By.id("btnLogin"));    
        }
    }
)

【讨论】:

    【解决方案2】:

    您用于 FluentWait 的参数和语法存在一些小问题。这是您的工作代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.FluentWait;
    import org.openqa.selenium.support.ui.Wait;
    //your code
    Wait<WebDriver> wait = new FluentWait(driver)    
        .withTimeout(30, TimeUnit.SECONDS)    
        .pollingEvery(5, TimeUnit.SECONDS)   
        .ignoring(NoSuchElementException.class);
    
        WebElement myLoginButton = wait.until(new Function<WebDriver, WebElement>() 
        {    
            public WebElement apply(WebDriver driver) 
            {    
                return driver.findElement(By.id("btnLogin"));    
            }
        });    
    

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多