Wait for a page to be loaded into Selenium

advertisements

I am using selenium with Java. I want to wait for page to load fully before doing any action on that page.

I have tried the following method, but it is failing to work as expected.

public void waitForElementToBeVisible(final WebElement element) {

    WebDriverWait wait = new WebDriverWait(WebDriverFactory.getWebDriver(), WEBDRIVER_PAUSE_TIME);

    wait.until(ExpectedConditions.visibilityOf(element));


This method will wait until element is visible. Firstly this method will check, whether element is available in html and whether it's display.. it will wait until element will display..

public void E_WaitUntilElementDisplay() throws Exception
{
    int i=1;
    boolean eleche,eleche1 = false;
    while(i<=1)
    {
            try{
                eleche = driver.findElements(by.xpath("path")).size()!=0;
            }catch(InvalidSelectorException ISExcep)
            {
                eleche = false;
            }
            if(eleche == true)
            {

                while(i<=1)
                {
                    try{
                        eleche1=driver.findElement(By.xpath("Path")).isDisplayed();
                    }catch(org.openqa.selenium.NoSuchElementException NSEE){
                        eleche1=false;
                    }
                    if(eleche1 == true)
                    {
                        i=2;
                        System.out.println("\nElement Displayed.");
                    }
                    else
                    {
                        i=1;
                        Thread.sleep(1500);
                        System.out.println("\nWaiting for element, to display.");
                    }
                }
            }
            else
            {
                i=1;
                Thread.sleep(1500);
                System.out.println("\nWaiting for element, to display.");
            }
    }
}