【问题标题】:Selenium webdriver java wait for element presentSelenium webdriver java等待元素存在
【发布时间】:2016-12-25 05:58:43
【问题描述】:

我试图让函数等待 Selenium 中的一个元素。

private WebElement  waitIsClickable(By by, int n) throws Exception{

        WebDriverWait wait= new WebDriverWait(driver,/*seconds=*/ n);
        wait.until(ExpectedConditions.elementToBeClickable(by));

        return driver.findElement(by);
}

但是当我想使用它时:

waitIsClickable(By.id("logIn"), 20).click();

我收到一个错误:

Error:(1057, 20) java: 类 Functions 中的方法 waitIsClickable 不能应用于给定类型;必需:org.openqa.selenium.By,int 找到:org.openqa.selenium。原因:实际和正式的参数列表 长度不同

【问题讨论】:

    标签: java selenium selenium-webdriver automated-tests


    【解决方案1】:

    您确定这是错误所在的行吗?你有这个方法的其他调用吗?根据错误描述,您似乎正在尝试这样调用:

    waitIsClickable(By.id("logIn")).click();
    

    【讨论】:

      【解决方案2】:

      您提供的错误堆栈跟踪状态需要两个参数,而您提供的是 By 对象。所以你需要再次检查你的来电参考。

      ExpectedConditions.elementToBeClickable 返回 WebElement 或抛出 TimeoutException 如果在等待期间不满足预期条件,因此无需再次查找元素。我建议您在waitIsClickable 中进行一些更正,如下所示:-

      private WebElement waitIsClickable(By by, long n) throws Exception {
          WebDriverWait wait= new WebDriverWait(driver, n);
          return wait.until(ExpectedConditions.elementToBeClickable(by));
       }
      
      By by = By.id("logIn");
      waitIsClickable(by, 20).click();
      

      【讨论】:

      • int 参数将被装箱为长。这不是问题(再次查看错误消息)。
      • @Guy 哦,是的,你是对的,错误状态期望两个参数,而找到一个。感谢指出..:)
      • @Guy 但是调用参考看起来没问题,因为 OP 提供了
      • 假设他/她实际上发布了女巫的线路给出了错误,我对此表示怀疑。
      猜你喜欢
      • 2016-07-28
      • 1970-01-01
      • 2012-03-26
      • 2016-06-14
      • 1970-01-01
      • 2012-07-28
      • 2013-08-06
      • 2012-06-14
      • 2013-04-24
      相关资源
      最近更新 更多