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?
As far as I know, there are 3 ways:
Implicit wait: (It's applicable for all elements on the page)
driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);
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")));
Using Thread:
Thread.sleep(NUMBER_OF_MILLIS);
I really would advise against using Thread.sleep(NUMBER_OF_MILLS). It will not be stable and you will hit occasions when the sleep is not long enough.
If you are simply waiting for the DOM to load, then a WebDriver event which triggers page load will always wait for the DOM to load before returning control.
However, if AJAX is used to change the HTML after DOM, then I would advise you to use WebDriverWait, and wait until a known event happens (e.g. Object appears in html, text changes, etc.)
If you take one thing away from this post, then please stop using Sleep!
Use class WebDriverWait
Selenium explicit / implicit wait
You can wait until the element you are expecting on the next page comes up. :
WebDriver _driver = new WebDriver();
WebDriverWait _wait = new WebDriverWait(_driver, TimeSpan(0, 1, 0));
_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));
Until
method. –
Comparable Try by using implicitlyWait for 60 sec. as follows in Selenium WebDriver:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//wait for 60 sec.
If you need to wait more make it 80, 90 and so on.
For Selenium RC you can use the code as below:
selenium.waitForPageToLoad("60000");//wait for 60 sec.
It can be done by using Thread as below:
Thread.sleep(NUMBER_OF_MILLIS);
For explicit wait in WebDriver, identify an element in loading page and write the code as below:
WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
I have used this approach. Try to figure out which element on the page is the last to load. By taking the locator of that element and checking it's existance using isDisplayed, you will be able to see when the entire page loads
Implicit Wait:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, 40);
This code is deprecated in Selenium 4.
Instead, use this,
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(40));
© 2022 - 2024 — McMap. All rights reserved.