我正在与 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
上收到错误类为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;
谁能帮帮我吗?
<小时/>更新
添加了answer关于 Selenium v3.11.0
中 FluentWait 的修改构造函数最佳答案
随着Selenium v3.11.0的可用性,FluentWait的构造函数已改变。现在 withTimeout
和 pollingEvery
的参数类型是 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"));
}
});
}
}
关于java - FluentWait 类型不是通用的;无法通过 Selenium 和 Java 使用 FluentWait 类的参数 <WebDriver> 错误进行参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775767/