Graeme Wilkinson
Graeme Wilkinson

Reputation: 419

Make selenium driver wait, on nothing, for x seconds

How can I make the selenium driver in Java wait on nothing for a few seconds, just to pause the driver?

Upvotes: 1

Views: 5849

Answers (5)

AnxiousDino
AnxiousDino

Reputation: 197

This is what I'm using in Python

import time

print "Start : %s" % time.ctime()
time.sleep( 5 )
print "End : %s" % time.ctime()

Output :

Start : Tue Feb 17 10:19:18 2009
End : Tue Feb 17 10:19:23 2009

Upvotes: 0

yangjiacheng
yangjiacheng

Reputation: 66

fun WebDriver.delay(timeout: Duration) {
    val until = System.currentTimeMillis() + timeout.toMillis()

    WebDriverWait(this, timeout).until {
        return@until until <= System.currentTimeMillis()
    }
}

It's kotlin extendsions code. normally method like this:

fun delay(driver: WebDriver, timeout: Duration) {
    val until = System.currentTimeMillis() + timeout.toMillis()

    WebDriverWait(driver, timeout).until {
        return@until until <= System.currentTimeMillis()
    }
}

Upvotes: 0

nicolastrres
nicolastrres

Reputation: 451

There are different ways to wait using selenium:

  • Explicit Waits: wait for a certain condition to occur before proceeding further in the code
    WebDriver driver = new FirefoxDriver();
    driver.get("http://somedomain/url_that_delays_loading");
    WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds

  • Implicit waits: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://somedomain/url_that_delays_loading");
    WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

Also you can use Thread.sleep(), this is not recommended but if you are just debugging this is the easiest way.

You can take a look to the Selenium documentation to understand better how to use waits.

Upvotes: 3

Graeme Wilkinson
Graeme Wilkinson

Reputation: 419

 try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

Seemed to do the trick.

Upvotes: 0

Rok Povsic
Rok Povsic

Reputation: 4895

Simply do Thread.sleep(1000) to sleep for 1 second.

Upvotes: 4

Related Questions