Selenium Webdriver 3.0.1:Selenium显示FluentWait类的错误[包括Selenium Java Client v3.11.0兼容的答案] [英] Selenium Webdriver 3.0.1: Selenium showing error for FluentWait Class [Selenium Java Client v3.11.0 compatible answer included]

查看:700
本文介绍了Selenium Webdriver 3.0.1:Selenium显示FluentWait类的错误[包括Selenium Java Client v3.11.0兼容的答案]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium Standalone Server 3.0.1 。我正在尝试向我的代码添加显式等待,以便在元素可见时通过xpath检测元素。为了获得一些Java帮助,我查找了 Selenium Standalone Server 3.0.1 的源代码,但无法找到它。我在 selenium-java-2.53.1 版本中找到了源代码。我下载了它,发现 selenium-java-2.53.1-srcs 并添加到我的 Eclipse IDE 。在 FluentWait 的帮助下,我只需复制粘贴我的 Eclipse IDE 中的代码并更改变量名称。

I am working with Selenium Standalone Server 3.0.1. I am trying to add an Explicit Wait to my code to detect an element through xpath when the element becomes visible. In order to get some Java help I looked out for the source code for Selenium Standalone Server 3.0.1 but was unable to find it. I found the source code in selenium-java-2.53.1 release. I downloaded it and found selenium-java-2.53.1-srcs and added to my Eclipse IDE. From the help of FluentWait, I simply copy pasted the code in my Eclipse IDE and changed the variable names.

文档中的示例代码如下:

The sample code in documentation is like:

   // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
      }
    });

但是当我实现这段代码时,只需复制粘贴它:

But when I implement this code, simply copy pasting it:

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

       WebElement element = wait.until(new Function<WebDriver, WebElement>()        {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.xpath("//p[text()='WebDriver']"));
         }
       });

我在 FluentWait 类上收到错误as FluentWait类型不是通用的;它不能参数化参数< WebDriver>

I am getting an error on FluentWait Class as The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>

以下是我的进口清单:

    import java.util.concurrent.TimeUnit;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Wait;
    import com.google.common.base.Function;

有人可以帮帮我吗?

添加回答关于 FluentWait 的修改构造函数 Selenium v​​3.11.0

Added an answer with respect to the modified constructor of FluentWait in Selenium v3.11.0

推荐答案

您需要在等待下面指定预期条件,修改后的代码是可以解决你的问题。

You need to specify expected condition inside the wait below is the modified code that could solve your problem.

代码:

import java.util.concurrent.TimeUnit;
import org.junit.Test;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

public class DummyClass
{
    WebDriver driver;
    @Test
    public void test()
    {
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

        until(new Function<WebElement, Boolean>() 
        {
            public Boolean apply(WebElement element)
            {
                return element.getText().endsWith("04");
            }

            private void until(Function<WebElement, Boolean> function)
            {
                driver.findElement(By.linkText("Sample Post2"));
            }
        }
    }
}

这篇关于Selenium Webdriver 3.0.1:Selenium显示FluentWait类的错误[包括Selenium Java Client v3.11.0兼容的答案]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆