Reputation: 85
I am trying to get my code to wait for an element to appear before trying to get the text from the element. If I step through the code allowing the element time to appear it works as expected, but if I run it without breakpoints the wait appears to be ignored and an exception is raised.
I don't understand why it is being ignored?
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement message = wait.Until(driver => driver.FindElement(By.ClassName("block-ui-message")));
string messageText = message.Text;
Upvotes: 1
Views: 1717
Reputation: 193058
As an alternative you can induce WebDriverWait for the ElementIsVisible()
and you can use the following Locator Strategy:
string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");
DotNetSeleniumExtras.WaitHelpers
with nuget
:Not that super clear what exactly you meant by specific using directive I need. In-case you are using SeleniumExtras and WaitHelpers you can use the following solution:
string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");
Upvotes: 2
Reputation: 419
Below sample code will wait until the element is presented
private bool IsElementPresent(By by)
{
try
{
_driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private void WaitForReady(By by)
{
var wait = new WebDriverWait(_driver, TimeSpan.FromHours(2));
wait.Until(driver =>
{
//bool isAjaxFinished = (bool)((IJavaScriptExecutor)driver).ExecuteScript("return jQuery.active == 0");
return IsElementPresent(by);
});
}
// ...
// Usage
WaitForReady(By.ClassName("block-ui-message")); // It will wait until element which class name is 'block-ui-message' is presented to page
Upvotes: 0