Showing posts with label wait for element present selenium. Show all posts
Showing posts with label wait for element present selenium. Show all posts

Thursday, 5 June 2014

Custom wait for element function for web driver

Web driver does have inbuilt functions to wait for the presence or even visibility of element on screen. However, at time we come across situations where non of the built in methods really work.

So here below is a custom method that is designed to use with any type of selenium webdriver frameworks. Lets have a look at the method::


public static void waitForEelement(final By locator, int timeOut) {

        for (int second = 0;; second++) {

            if (second >= timeOut) fail("timeout waiting for element");

            try {

                 if ((getSelenium().getWrappedDriver().findElements(locator).size()) > 0){

                     break;

                }

                } catch (Exception e) {}

            try {

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                 e.printStackTrace();

            }

        }

      

    }


This method can be called upon as follows:

waitForEelement(By.xpath("//div[@class='signin']"),60);