Have you been in the following situations:
- Selenium driver fails to find an element because the web page uses AJAX
- The web element is present but selenium driver fails to find the element intermittently
These were two situations which prompted me to write my own custom WaitFor class which has two methods:
- Wait for a javascript code to execute until a specified time-out
- Check if element is present until a specified time-out
Here is the WaitFor Class:
(I did see these solutions in a blog discussions or forum. As soon as I spot the source, I will reference it).
Here are some of the reasons it works for me:
- Even though Webdriver 2 is supposed to wait for the page to load before checking for element to be present, it does not work always. The WaitForElementPresent method waits for the specified timeout and keeps checking every 500 ms until the timeout expires
- There are multiple ways to wait for AJAX functionality to finish loading, JSWait method above works like a charm for me
Here is how to call these methods from code:
- Selenium driver fails to find an element because the web page uses AJAX
- The web element is present but selenium driver fails to find the element intermittently
These were two situations which prompted me to write my own custom WaitFor class which has two methods:
- Wait for a javascript code to execute until a specified time-out
- Check if element is present until a specified time-out
Here is the WaitFor Class:
(I did see these solutions in a blog discussions or forum. As soon as I spot the source, I will reference it).
public class WaitFor
{
private bool JSEval(IWebDriver driver, string script)
{
object result = ((IJavaScriptExecutor)driver).ExecuteScript(script);
if (result == null)
{
return false;
}
else
{
return (bool)result;
}
}
public void JSWait(IWebDriver driver, string script, long timeout)
{
int interval = 500;
long waited = 0;
while (!JSEval(driver,script) && waited < timeout)
{
Thread.Sleep(interval);
waited += interval;
}
}
private bool IsElementPresent(IWebDriver driver, By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
public void WaitForElementPresent(IWebDriver driver, By by, long timeout)
{
int interval = 500;
long waited = 0;
while (!IsElementPresent(driver,by) && waited < timeout)
{
Thread.Sleep(interval);
waited += interval;
}
}
}
Here are some of the reasons it works for me:
- Even though Webdriver 2 is supposed to wait for the page to load before checking for element to be present, it does not work always. The WaitForElementPresent method waits for the specified timeout and keeps checking every 500 ms until the timeout expires
- There are multiple ways to wait for AJAX functionality to finish loading, JSWait method above works like a charm for me
Here is how to call these methods from code:
WaitFor ws = new WaitFor();
// Check for element to be present, wait for 10 seconds before timing out
ws.WaitForElementPresent(driver, By.XPath("//span[text()='Find Element']"), 10000);
// Wait for the javascript code to return true before executing the next statement
ws.JSWait(driver, "return Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();", 500);
ws.JSWait(driver, "return !(Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack());", 500);