I've written a C# extension method that accepts a By
for an element and tries to wait for up to 5 seconds while repeatedly polling the page to see if the element is present.
Here's the code:
public static IWebElement FindWithWait(this ISearchContext context, By by)
{
var wait = new DefaultWait<ISearchContext>(context)
{
Timeout = TimeSpan.FromSeconds(5)
};
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return wait.Until(ctx =>
{
Console.WriteLine($"{DateTimeOffset.Now} wait is trying...");
return ctx.FindElement(by);
});
}
For the purpose of this question, here's how I invoke the method:
Console.WriteLine($"{DateTimeOffset.Now} before");
try
{
var element = driver.FindWithWait(By.Id("not_in_page"));
}
catch (Exception ex)
{
Console.WriteLine($"{DateTimeOffset.Now} exception:" + ex);
}
Console.WriteLine($"{DateTimeOffset.Now} after");
Given that an element with id #not_in_page
does not exist in the page and that the default polling time for the Until()
method is 500 milliseconds, I would expect the code to print out something like:
11/4/2019 11:20:00 AM +02:00 before
11/4/2019 11:20:00 AM +02:00 wait is trying...
11/4/2019 11:20:01 AM +02:00 wait is trying...
11/4/2019 11:20:01 AM +02:00 wait is trying...
11/4/2019 11:20:02 AM +02:00 wait is trying...
11/4/2019 11:20:02 AM +02:00 wait is trying...
11/4/2019 11:20:03 AM +02:00 wait is trying...
11/4/2019 11:20:03 AM +02:00 wait is trying...
11/4/2019 11:20:04 AM +02:00 wait is trying...
11/4/2019 11:20:04 AM +02:00 wait is trying...
11/4/2019 11:20:05 AM +02:00 wait is trying...
11/4/2019 11:20:05 AM +02:00 after
However, what I'm actually getting is :
11/4/2019 11:20:00 AM +02:00 before
11/4/2019 11:20:00 AM +02:00 wait is trying...
11/4/2019 11:21:00 AM +02:00 exception: OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL ######### timed out after 50 seconds. ---> #########
11/4/2019 11:21:00 AM +02:00 after
Note that the polling seems to happen only once and that the exception was thrown 60 seconds after starting to poll.
Is wait.Until()
subject to an implicit wait of 60 seconds? How can I instead make it ignore it and poll once every 500 milliseconds?