Java – How to wait a page before going to another page in Selenium WebDriver using Java

eclipsejavaseleniumselenium-webdriver

I am new in Selenium. I am using Selenium WebDriver with Java. I'm using eclipse as IDE. I have written some code for Login page and it is run successfully. Now I want to go to desired page after successful login, but I want to wait for few time before transiting another page. How can I wait a page before loading another page?

Best Answer

As far as I know, there are 3 ways:

  1. Implicit wait: (It's applicable for all elements on the page)

    driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);

  2. Explicit wait: (Applicable for a particular element)

    WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);

More specific code is as below:

WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
  1. Using Thread:

    Thread.sleep(NUMBER_OF_MILLIS);