Selenium Webdriver 3.0.1:Selenium显示FluentWait类的错误

我正在使用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 wait = new FluentWait(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } }); 

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

  Wait wait = new FluentWait(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement element = wait.until(new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.xpath("//p[text()='WebDriver']")); } }); 

我在FluentWait类上收到错误, FluentWait The type FluentWait is not generic; it cannot be parameterized with arguments The type FluentWait is not generic; it cannot be parameterized with arguments

这是我的导入列表:

  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的修改构造函数的答案

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

码:

 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 wait = new FluentWait(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); until(new Function() { public Boolean apply(WebElement element) { return element.getText().endsWith("04"); } private void until(Function function) { driver.findElement(By.linkText("Sample Post2")); } } } } 

随着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 wait = new FluentWait(driver) .withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofMillis(500)) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.name("q")); } }); } } 
Interesting Posts