我一直在努力搜索如何等待特定元素加载到浏览器中。然而,大多数代码都在python和C#中,我正在尝试转换,但仍然无法转换。
-
我找不到ExpectedConditions请让我知道我需要导入哪个引用,因为我一直在寻找命名空间中的ExpectedCondition仍然找不到它。
-
有人能分享一个如何使用webdriverwait的代码或链接吗?因为我仍然无法理解。我想做的是等待一个元素被加载到浏览器中,如果元素不在页面中,就会做其他事情。现在我用的是试试看,但是,有时网速真的等不到页面满负荷,直接显示出页面中没有电子邮箱的例外。
代码:
WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("MainContent_lblLandmarkUPRN")));
您可以使用以下函数等待元素可见
public static IWebElement WaitforElement(this IWebDriver driver, By by, int timeoutInSeconds = 5, bool checkIsVisible = false)
{
IWebElement element;
driver.Sync();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
try
{
if (checkIsVisible)
{
element = wait.Until(ExpectedConditions.ElementIsVisible(by));
element = wait.Until(ExpectedConditions.ElementToBeClickable(by));
}
else
{
element = wait.Until(ExpectedConditions.ElementExists(by));
}
}
catch (NoSuchElementException) { element = null; }
catch (WebDriverTimeoutException) { element = null; }
catch (TimeoutException) { element = null; }
catch (StaleElementReferenceException) { element = null; }return element;
}