簡體   English   中英

FluentWait 類型不是通用的; 它不能用 arguments 參數化<webdriver> FluentWait 錯誤 Class 到 Selenium 和 Java</webdriver>

[英]The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> error for FluentWait Class through Selenium and Java

我正在使用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']"));
         }
       });

我在FluentWait Class 上收到錯誤消息,因為 FluentWait The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> 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 v3.11.0中添加了關於FluentWait的修改構造函數的答案

隨着Selenium v​​3.11.0的推出, FluentWait的構造函數發生了變化。 現在withTimeoutpollingEvery的參數類型是Duration 這是修改后的實現:

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

    }

}

我也fluentwait了同樣的錯誤,后來我注意到我使用的類名是fluentwait 更改類名后,它工作正常。

您需要在下面的等待中指定預期條件是可以解決您的問題的修改后的代碼。

代碼:

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

最簡單的解決方案是使用其他方法實現:

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

表單withTimeout(Duration timeOut)仍在使用withTimeout(Duration timeOut)棄用

我也面臨同樣的錯誤,即The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>但我注意到一個愚蠢的錯誤,我的主類也被命名為 FuentWait 並且它不是通用的。 我更改了它的名稱,錯誤消失了。

我得到了同樣的錯誤,因為我將我在 eclipse 中創建的 class 命名為“FluentWait”以實現流暢的等待。 嘗試將 class 重命名為其他名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM