12

I realize that Selenium has a default value for implicit waits, but how do I get this value if I change it? For example:

driver.implicitly_wait( 13 );

How do I later get the 13 value from the driver?

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
sam-6174
  • 3,104
  • 1
  • 33
  • 34

8 Answers8

10

Unfortunately there's no getter for that.

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html

There isn't for explicit waits either.

http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Wait.html

Community
  • 1
  • 1
JaneGoodall
  • 1,478
  • 2
  • 15
  • 22
  • 3
    +1 for right answer, but that is really unfortunate :(, Why would they not have a getter for that value... – David Rogers Oct 07 '16 at 14:59
  • Those links don't work anymore. The docs might not show any getters, but we could look at the source code to see if there is a way to make our own getter. I did that and I don't see a way to make our own getter. The implicit wait is effectively hidden from us - https://stackoverflow.com/a/60919695/1521751 – armani Mar 29 '20 at 19:32
6

I know I'm a couple years late, and @JaneGoodall is not wrong -- there is no built-in function for that. But it's not impossible!

It's not very difficult to create your own versions of the WebDriver interface and browser-specific driver class. And then, you can put whatever code you want into the driver!

Example:

MyDriver.java (specialized version of WebDriver, not quite mandatory but a very good idea):

public interface MyDriver extends WebDriver {
    void setWait(int timeout);
    int getWait();
}

MyChrome.java (specialized version of ChromeDriver -- works the same for any browser)

public class MyChrome extends ChromeDriver implements MyDriver {
    int timeout = 0;
    public void setWait(int timeout) {
        this.timeout = timeout;
        this.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
    }
    public int getWait() {
        return timeout;
    }
}

And now, to use it, MyProgram.java:

public class MyProgram {
    MyDriver driver = new MyChrome();
    driver.setWait(10);
    assert(driver.getWait() == 10);
}

I hope this is helpful!

forresthopkinsa
  • 1,339
  • 1
  • 24
  • 29
  • 1
    It would be a lot easier to just set the desired wait time using a variable. Then you can reference the variable any time you want. – JeffC May 30 '17 at 20:03
  • Sure, but sometimes you need that value to be packaged with the driver object itself. This solution is much cleaner than passing a variable around to wherever the driver is used. – forresthopkinsa Jun 06 '17 at 16:47
  • @forresthopkinsa - Thanks for your answer. Why do you need the value to be packaged with the driver object itself ? Also, are there any good justifications to be able to get the implicit waits ? – armani Mar 29 '20 at 19:38
  • @armani There are a number of situations where you're passing the driver around to a lot of places and you can't (or don't want to) send around a configuration object with it. An easy example is when using a DI container -- you just inject the driver and you have the info you need. This method also guarantees that the wait getter is in sync with the actual wait value. As for *why*, well, one scenario is that you need to know the implicit wait to be able to time other code properly. I think the activity on this question is evidence that there's a need to get implicit wait values. :) – forresthopkinsa Mar 30 '20 at 09:28
3

For those who came here from google. In 2018 it seems like there is a method to get those timeouts at least in javascript (I know question was about java):

const {implicit, pageLoad, script} = await driver.manage().getTimeouts();

Hope this will help.

Alex Povar
  • 4,890
  • 3
  • 29
  • 44
1

Many years later, in Python, using selenium 4.4.3 you can access the timeouts very easily via simply:

print(driver.timeouts.implicit_wait)
print(driver.timeouts.page_load)
print(driver.timeouts.script)

Note that when I call driver.implicitly_wait(60), it changed the implicit_wait value only.

Also, there is no such function as driver.manage() in Python, as in the Java answers above.

supermitch
  • 2,062
  • 4
  • 22
  • 28
0

TL;DR - This is not a solution to get implicit waits. You cannot get the implicit wait in Java even today, without using a workaround like this.

In 2020, selenium 3.141.59 still does not have a getter for any timeouts. The WebDriver interface has a nested interface Timeouts which does not define any getters. RemoteWebDriver, which is the parent of Chrome and Firefox drivers, implements the WebDriver interface and it does not add a getter for timeouts.

RemoteWebDriver implements WebDriver.Timeouts, but it does not store the value of implicit timeout anywhere, as you can see from the code below.

protected class RemoteTimeouts implements Timeouts {

  public Timeouts implicitlyWait(long time, TimeUnit unit) {
    execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
        "implicit", TimeUnit.MILLISECONDS.convert(time, unit)));
    return this;
  }

  public Timeouts setScriptTimeout(long time, TimeUnit unit) {
    execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
        "script", TimeUnit.MILLISECONDS.convert(time, unit)));
    return this;
  }

  public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
    execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
        "pageLoad", TimeUnit.MILLISECONDS.convert(time, unit)));
    return this;
  }
} // timeouts class.

The execute() method in the RemoteWebDriver takes the wait inside a Map of parameters, but it does not make that map or the wait settings accessible to us via a getter.

  protected Response execute(String driverCommand, Map<String, ?> parameters)
  //Open the source code to see why you can't make your own getter for implicitWait.
armani
  • 156
  • 1
  • 3
  • 13
0

This can print real timeout value (plus calculating time, usually within 100ms):

public void getCurrentWaitTimeout() {
    long milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli();
    driver.findElements(By.cssSelector(".nonExistingElement"));
    milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli() - milliseconds;
    log.info("Current waiting timeout is {} milliseconds", milliseconds);
}

So you can always call such a method to be sure you know actual timeout, not the value you tried to set.

Dima
  • 1
  • 1
0

For Java version of Selenium, org.seleniumhq.selenium:selenium-api:4.0.0-beta-4 allows you to get the current implicit wait duration:

WebDriver.manage().timeouts().getImplicitWaitTimeout()

With this method, it makes possible to temporarily change the timeout to let's say 1 second and restore it afterwards:

final Duration originalTimeout = driver.manage().timeouts().getImplicitWaitTimeout();
driver.manage().timeouts().implicitlyWait(Duration.of(1, ChronoUnit.SECONDS));
... // do something
// restore the original timeout
driver.manage().timeouts().implicitlyWait(originalTimeout);

Probably, this functionality is present even before selenium-api:4.0.0-beta-4.

Andrew
  • 2,148
  • 5
  • 23
  • 34
0

I get the defined implicitTimeout with:

driver.manage().timeouts().getImplicitWaitTimeout().getSeconds()