Let's say we have a web page with one button and if we click on the button a text message will be displayed. We have written a selenium script to click on the button and capture the text displayed. But we are constantly getting 'NoSuchElementFound' exception.
Why do we need waits in selenium?
Have you ever faced this kind of situation where our selenium script &locators are perfect and still getting exceptions? This could be due to synchronization in the selenium web driver and web page. Web elements take some time to show up / appear when we perform some actions. So webdriver should wait some time for web elements to load.
We can’t really estimate the exact response time that action takes on a web page. This is where waits in selenium webdriver come in. What are the waits in selenium webdriver? Waits in selenium webdriver providers an ability to halt driver for a period of time. Wait specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
What is Implicit Wait?
Ans: Implicit wait is a type of wait in selenium, which waits for a certain amount of time before throwing an exception (ex: NoSuchElementException when searching for an element). The default time setting for the implicit wait is 0 (zero), which means if we don't initialize implicit wait it will not wait for any driver operation so we need to set some wait time to make WebDriver wait for the required time. Once we declare the implicit wait driver will wait for the given time for each and every operation. We should not use implicit wait multiple times however recent one overrides the previously defined time.Syntax:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Code:
System.setProperty("webdriver.chrome.driver","enter chromedriver path here");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com");
//There is no element with name 's' on google page
driver.findElement(By.name("s"));
//so we will get NoSuchElementException after 30 seconds
What is Explicit Wait? The explicit wait is used when we want to wait for a particular operation or expected condition. It is a smarter wait compared to implicit wait. If we define a certain time in explicit wait for an expected condition then it will wait till that operation only. The lifetime of explicit wait is till the driver's operation.
Syntax:
WebDriverWait wait=new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "valid locator"));
All Expected Conditions that are available in Explicit Wait.
alertIsPresent()
elementSelectionStateToBe()
elementToBeClickable()
elementToBeSelected()
frameToBeAvaliableAndSwitchToIt()
invisibilityOfTheElementLocated()
invisibilityOfElementWithText()
presenceOfAllElementsLocatedBy()
presenceOfElementLocated()
textToBePresentInElement()
textToBePresentInElementLocated()
textToBePresentInElementValue()
titleIs()
titleContains()
visibilityOf()
visibilityOfAllElements()
visibilityOfAllElementsLocatedBy()
visibilityOfElementLocated()
Differences between Implicit Wait vs Explicit Wait
Implicit WaitExplicit WaitWe initialize implicit wait once We initialize explicit wait for each expected condition.
We don't have any ExpectedConditions We need to specify ExpectedConditions
Implicit Wait time is applied to all the elements in the script Explicit Wait time is applied only to those elements which are intended by us
Syntax:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Syntax: WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "valid locator"));
Can we mix Implicit Wait and Explicit Wait ?
No, it is not recommended to use implicit and explicit waits together as it can cause unpredictable wait times.Best practice is to use any one of the waits as per need.