I\'ve started a Selenium project in C#. Trying to wait for page to finish loading up and only afterwards proceed to next action.
My code looks like this:
<
Because of its simplicity I like this solution. Also it has the benefit of avoiding excessive waits and taking the guess work out of what might be the upper wait limit:
public bool WaitToLoad(By by)
{
int i = 0;
while (i < 600)
{
i++;
Thread.Sleep(100); // sleep 100 ms
try
{
driver.FindElement(by);
break;
}
catch { }
}
if (i == 600) return false; // page load failed in 1 min
else return true;
}
Which can be modified to also include a "timer" if monitoring page load latency is needed:
public int WaitToLoad(By by)
{
int i = 0;
while (i < 600)
{
i++;
Thread.Sleep(100); // sleep 100 ms
try
{
driver.FindElement(by);
break;
}
catch { }
}
return i; // page load latency in 1/10 secs
}