WebDriver.Wait Not Awaiting Specified Element
Query:
My code attempts to await the appearance of an element before retrieving its text content. While stepping through the code, it functions properly. However, when run without breakpoints, the wait appears to be bypassed and an exception is thrown.
Why is the wait being neglected?
Code Snippet:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); IWebElement message = wait.Until(driver => driver.FindElement(By.ClassName("block-ui-message"))); string messageText = message.Text;
Response:
An alternative approach is to use WebDriverWait's ElementIsVisible() method in combination with a modified Locator Strategy:
string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");
Using DotNetSeleniumExtras.WaitHelpers with NuGet:
If using SeleniumExtras and WaitHelpers, the code can be modified as follows:
string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");
The above is the detailed content of Why Does My WebDriverWait Seem to Be Ignored When Running Without Debugging?. For more information, please follow other related articles on the PHP Chinese website!