天天看點

java selenium 判斷元素存在_java – Selenium WebDriver – 測試元素是否存在

有沒有辦法如何測試一個元素是否存在?任何findElement方法将在異常中結束,但這不是我想要的,因為它可能是一個元素不存在,這是好的,這不是測試的失敗,是以異常不能是解決方案。

我發現這個文章:Selenium c# Webdriver: Wait Until Element is Present

但這是為C#,我不是很擅長。任何人都可以将代碼翻譯成Java?對不起,我試過它在Eclipse,但我不能把它正确的Java代碼。

這是代碼:

public static class WebDriverExtensions

{

public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)

{

if (timeoutInSeconds > 0)

{

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));

return wait.Until(drv => drv.FindElement(by));

}

return driver.FindElement(by);

}

}