Most of the web applications are developed using JS based frameworks which include Ajax and Javascripts. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals.

Not only it makes this difficult to identify the element but also if the element is not located it will throw an "NoSuchElementException" exception. Using Waits, we can resolve this problem.

  • Implicit Wait
  • Explicit Wait
  • Fluent wait
  • Sleep
  • Page Load Timeout
  • Script Load Timeout

Implicit Wait :

In Implicit wait, if selenium Kotlin cannot find an element in the Document Object Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM.

Implicit wait: Implicit wait is set for the entire duration of your webdriver and is set at the start of your program.

What it does is, in case while executing, if your webdriver doesn't finds any element then instead of throwing an exception, implicit wait makes your driver to wait for the specified wait time and then try to find the element once again. If the driver still does not finds the element then it throws exception.

Implicit wait does the same for all the elements in your program, so you just have to set it once.

Ex: Let's say that you give an Implicit wait of 20 sec, now while executing, your webdriver is not able to find an element, but since you've set implicit wait, the webddriver waits instead of throwing exception.

Lets say the time the webdriver searched for element is t=0.Now the webdriver waits for 20sec, and at t=21 driver tries to find the element again, if it finds the element then it performs the particular action on the element and if it doesn't, it throws exception.

The Implicit wait may slow down your tests, because once set, the implicit wait is set for the life of the Web Driver object's instance.

Implicit Wait is only applicable to finElement and findElements methods only

							fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    // set implicitly wait time for 1 Minute
    driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES)
    // open webpage
    // find the dropdown using xpath
    val dropdownElement = driver.findElement(By.xpath("//select[@id='second']"))
    // create object for Select class
    val dropdown = Select(dropdownElement)
    // select options from the dropdown options which have value as 'donut', 'burger'

Explicit Wait :

Explicit wait works similar to Implicit Wait, but unlike Implicit wait, it works only on the particular condition, on which it is set, rather than on all elements in your code.

Also explicit wait tells the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an exception.

Ex: If Explicit wait of 10 sec is set on an element with an expected condition and while execution the condition is false, at t=0, then, it makes your driver to wait for 10 sec and on t=11, the webdriver checks the condition again.

If the condition is true then it moves to the next step in your code and if it is false, it throws TimeoutException exception. Below code will wait for element to become clickable

							fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    val wait = WebDriverWait(driver, 30 /*timeout in seconds*/)

Methods present in ExpectedConditions :

There are so many methods present in the ExpectedConditions class, but Ihave listed few of them.

  • alertIsPresent()
  • elementSelectionStateToBe(locator, selected)
  • elementToBeClickable(locator)
  • elementToBeSelected(locator)
  • frameToBeAvailableAndSwitchToIt(locator)-(will see in advanced topic)
  • presenceOfElementLocated(locator)
  • textToBePresentInElement(locator, text)
  • title()
  • titleIs(title)
  • visibilityOf(element)

Thread.sleep() :

Another way to implement waits is by calling thread.sleep() function however, it is not recommended because this is very static if you provide sleep(100000) of 100 seconds then it will halt the total program for 100 seconds. The time has to be specified in milliseconds.

							fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    //wait for 5 seconds
    //send text to the search bar
    driver.findElement(By.name("search bar")).sendKeys("selenium-webdriver");
    //click on the search button

PageLoad Timeout :

Selenium defines different timeouts and wait mechanisms. One of the timeouts is focused on the time a webpage needs to be loaded – the pageLoadTimeout limits the time that the script allots for a web page to be displayed.

If the page loads within the time then the script continues. If the page does not load within the timeout the script will be stopped by a TimeoutException. Below code expects the google page to load in 4 seconds

fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below

Join My Facebook Group
Join Group