×
☰ See All Chapters

Unconditional wait in Selenium WebDriver

What if we don’t get any stable condition but we need to wait or pause the execution for some time, solution is making the java thread to sleep for desired time. i.e. Thread.sleep(timeout);

Selenium Unconditional wait Example

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                // Launch website

                driver.get("https://www.registration.tools4testing.com/");

               

                // Click on the Login Button

                driver.findElement(By.id("loginopener")).click();

 

                // Focus on the dialog window by click on dialog window tittle

                driver.findElement(By.xpath("//*[@id=\"ui-id-1\"]")).click();

               

                // Enter user name

                driver.findElement(By.id("loginUsername")).sendKeys("manu.m@tools4testing.com");

               

                // Enter user password

                driver.findElement(By.id("loginPassword")).sendKeys("hello");

               

                // Click on the Login Button

                driver.findElement(By.id("loginButton")).click();

               

                //wait some time to get login response

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                driver.findElement(By.xpath("//*[@id=\"ui-id-3\"]")).click();

                driver.findElement(By.xpath("//*[@id=\"loginSuccessDialog\"]/div/span/input")).click();

               

                //close the driver

                driver.quit();

               

        }

}

All the below wait configurations are made to wait webdriver by specifying conditions.

Condition

WebDriver Expected Conditions

An element is visible and enabled

elementToBeClickable(By locator)

An element is selected

elementToBeSelected(WebElement element)

Presence of an element

presenceOfElementLocated(By locator)

Specific text present in an element

textToBePresentInElement(By locator, java.lang.String text)

Element value

textToBePresentInElementValue(By locator, java.lang.String text)

Title

titleContains(java.lang.String title)

 


All Chapters
Author