我發現ExpectedConditions在方法中將 an作為引數傳遞給wait.until(). 該wait.until()預期的函式中過去了。我是比較新的Java和希望得到的幫助。
違規代碼:
public void waitUntil(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions) {
if (seconds == 0 || errorMessage.isEmpty()) throw new IllegalArgumentException();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(seconds));
wait.withMessage(errorMessage);
// this throws a compiler error.
wait.until(expectedConditions);
}
wait.until()期望將一個函式傳遞給它,它看起來像ExpectedConditions.urlToBe("http://www.test.com").
我正在嘗試創建一個可以在任何 ExpectedCondition(即 urlToBe、alertIsPresent 等)可以被傳入的地方呼叫的方法。
謝謝你。
uj5u.com熱心網友回復:
(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions)
首先,型別expectedConditions是錯誤的。
您將其宣告為ExpectedConditions,代表 util 類。
您實際上想要ExpectedCondition,所有方法都ExpectedConditions回傳的型別。
但僅僅改變它ExpectedCondition是不夠的。因為你會收到一個關于 的警告Raw type,因為ExpectedCondition是一個泛型類。
所以你必須宣告類的型別引數,并且因為你想包含一切,所以你使用通配符 ?
最后,引數應該是 ExpectedCondition<?> expectedConditions
uj5u.com熱心網友回復:
WebDriverWait 有一個建構式,它被多載需要很長時間作為第二個引數。你不需要使用Duration.ofSeconds(seconds)
還,
The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedConditions)
使固定 :
你應該使用 expectedConditions 和一個像
- elementToBeClickable
- 元素可見性
等等..
代碼 :
public void waitUntil(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions) {
if (seconds == 0 || errorMessage.isEmpty()) throw new IllegalArgumentException();
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.withMessage(errorMessage);
WebElement elemenet = wait.until(expectedConditions.elementToBeClickable(By.xpath("//some xpath")));
}
內部多載方法:
*/
public WebDriverWait(WebDriver driver, long timeOutInSeconds) {
this(
driver,
java.time.Clock.systemDefaultZone(),
Sleeper.SYSTEM_SLEEPER,
timeOutInSeconds,
DEFAULT_SLEEP_TIMEOUT);
}
uj5u.com熱心網友回復:
我在您的代碼塊中沒有看到任何錯誤。但是,您需要確保以下幾點:
在使用ExpectedConditions 時,您必須進行以下匯入:
import org.openqa.selenium.support.ui.ExpectedConditions;當您使用Selenium 4.0.0 時,您需要使用
guava-31.0.1-jre.jar
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/350234.html
