如果您使用的是 Maven,请注意 order of the dependencies do matter。
例如:
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/Users/me/geckodriver");
final WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
final WebDriverWait wait = new WebDriverWait(driver, 5);
final By feelLuckyXpath = By.xpath("//div[@class='jsb']/center/input[@type='submit' and @name='btnI']");
wait.until(ExpectedConditions.visibilityOfElementLocated(feelLuckyXpath)).click();
driver.close();
}
此代码适用于以下 maven 依赖项:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>
但它可能会因重新排序而失败:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
在这种情况下,因为google-api-client 包含:
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
作为在selenium 库中隐藏guava 库的依赖项。
在这种情况下,错误是:
不存在类型变量 V 的实例,因此 ExpectedCondition ...
直到类 org.openqa.selenium.support.ui.FluentWait 中的方法不能应用于给定类型;
必需:java.util.function.Function
找到:org.openqa.selenium.support.ui.ExpectedCondition
原因:无法推断类型变量 V
(参数不匹配;org.openqa.selenium.support.ui.ExpectedCondition 无法转换为 java.util.function.Function)