6

The following is the java code to wait for a page loading in Selenium RC:

selenium.waitForPageToLoad("30000");

What is the equivalent java code in Selenium WebDriver?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176

2 Answers2

11

2 approaches:

  1. If you need to wait exactly 60 sec you could use Thread.sleep(60000)

  2. If you want to make sure that the page is loaded (it could be less than or greater than 60 sec) I would recommend the below approach:

Identify an element in the landing page & wait for it to be clickable. You are then sure that the page has been loaded.

WebDriverWait wait = new WebDriverWait(driver,120);
wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));

WebDriver waits for a max of 120 sec. for the element to be clickable. If the element is clickable before that, your test would progress.

praneel
  • 1,842
  • 4
  • 19
  • 24
  • Yes, Thread.sleep(60000) is better and simple code to wait for a certain period before loading next page. Explicit Wait is better to wait for element. – Ripon Al Wasim Aug 23 '13 at 04:07
  • how to handle the case where element takes more than 120 seconds to load, how to handle such cases where we dont know the wait time, cant it wait undefinitely for page to load? – Chetan Aug 13 '18 at 13:47
  • does `browser.wait(function () { return element(by.selector('element')).isPresent(); })` work in this case? – HardikT Oct 26 '18 at 06:56
1
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Ben
  • 54,723
  • 49
  • 178
  • 224
RoDev
  • 61
  • 8
  • Thanks for your help. The above code is applicable for all elements. But I want to wait for 60 sec. before loading another page after clicking Login button (assuming it's a login page --> Click Login button after filling up necessary information --> I like to wait 60 sec. before loading desired page). – Ripon Al Wasim Aug 22 '13 at 11:30
  • implicitlyWait is global. You wouldn't want to wait for elements that doesn't need waiting. – Mario Galea Jan 28 '16 at 11:12