注意如果您使用的是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
庫。
在這種情況下,錯誤是:
no instance(s) of type variable(s) V exist so that ExpectedCondition<> ...
method until in class org.openqa.selenium.support.ui.FluentWait cannot be applied to given types; required: java.util.function.Function found: org.openqa.selenium.support.ui.ExpectedCondition reason: cannot infer type-variable(s) V (argument mismatch; org.openqa.selenium.support.ui.ExpectedCondition cannot be converted to java.util.function.Function)
我去官方硒聊天室的人告訴我,做你做同樣的事情。它也適用於我。我來這裏發佈一個答案,但你已經做到了:)。 –