1

我正在与Selenium Standalone Server 3.0.1. 我正在尝试在Explicit Wait我的代码中添加一个以在元素变得可见时通过 xpath 检测元素。为了获得一些 Java 帮助,我查找了源代码, Selenium Standalone Server 3.0.1但找不到。selenium-java-2.53.1我在发布中找到了源代码。我下载了它并找到selenium-java-2.53.1-srcs并添加到我的Eclipse IDE. 在 的帮助下FluentWait,我只是复制粘贴了我的代码Eclipse IDE并更改了变量名。

文档中的示例代码如下:

   // 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"));
      }
    });

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

       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']"));
         }
       });

我在FluentWaitClass上遇到错误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;

谁能帮帮我?


更新

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

4

5 回答 5

3

随着Selenium v​​3.11.0 的推出,FluentWait构造函数发生了变化。withTimeout现在and的参数类型pollingEveryDuration。这是修改后的实现:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import com.google.common.base.Function;

public class Fluent_Wait {

    public static void main(String[] args) {


        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
            driver.get("https://www.google.com");
            // Waiting 30 seconds for an element to be present on the page, checking
            // for its presence once every 500 milliseconds.
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofSeconds(30))
            .pollingEvery(Duration.ofMillis(500))
            .ignoring(NoSuchElementException.class);

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

    }

}
于 2018-04-06T10:27:25.450 回答
2

我也面临同样的错误,后来我注意到我使用的类名是fluentwait. 更改类名后,它工作正常。

于 2021-05-09T15:03:59.850 回答
1

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

代码:

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"));
            }
        }
    }
}
于 2017-02-16T16:33:41.640 回答
0

最简单的解决方案是使用另一种方法实现:

withTimeout(Duration.ofSeconds(10))
            .pollingEvery(Duration.ofSeconds(2))

该表格withTimeout(Duration timeOut) 仍在使用且未弃用

于 2018-11-24T12:28:38.433 回答
0

我也面临同样的错误,The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>但我注意到一个愚蠢的错误,我的主类也被命名为 FuentWait,它不是通用的。我更改了它的名称,错误消失了。

于 2022-01-02T16:44:05.930 回答