1

I am using WebDriverWait to find an Element which will be visible after few seconds. I have declared time for 10sec max to wait for that particular element

WeDriverWait wait = new WebDriverWait(driver, 10)
                    .until(ExpectedConditions.visibilityOfElement("path"));

now my expection is to , if element is not visible withing 10 seconds then i should get NoSuchElementException after 11th second, but it takes more than 30secs(approx) and throws TimeOut Exception.

Thanks in advance for Suggestion and clarification ...!!!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 4
    `TimeOutException` is the expected behavior when using `WeDriverWait`. It take 30 seconds instead of 10 probably because you set `implicitlyWait` somewhere. – Guy Aug 05 '19 at 08:08
  • @Guy You should add it as an answer. That's perfect – Fenio Aug 05 '19 at 08:52
  • While Initializing a browser, am using ImplicitWait for 30seconds. After that m no where using that. Rest of the times I am using FluentWait. Initial browser it was told me that we have to use Implicit for only one time to give time to load all the WebElements??? Is it bad not to use implicit at all, if we don't and just focus on Explicit/fluent while initializing the browser ...? Does it work or cause some issue cause the page has not loaded completely? – naveen kumar Aug 06 '19 at 06:12

2 Answers2

1

You saw it right. As per the documentation of WebDriverWait() the constructors are:

  • WebDriverWait(WebDriver driver, java.time.Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut)
  • WebDriverWait(WebDriver driver, long timeOutInSeconds)
  • WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)

For a successful WebDriverWait the desired element/elements is/are returned, whereas incase of a failure timeout exception is thrown.


However there is a small issue in your code block:

WeDriverWait wait = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElement("path"));    

Instead of an instance of WeDriverWait, the desired element is returned. So you need to change the line as:

WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElement("path"));

In a stepwise manner:

WeDriverWait wait = new WebDriverWait(driver, 10)
WebElement element = wait.until(ExpectedConditions.visibilityOfElement("path"));

It is not clear from your question why it takes more than 30 secs(approx) to throw the TimeOutException but the most possible cause is, though you have set the duration of WebDriverWait as 10 seconds, you have also have induced ImplicitlyWait as well and as per the documentation WARNING: Do not mix implicit and explicit waits! Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    as @DebanjanB mentioned, please lookout for implicit wait which u must have given after initializing browser, i had a similar issue where after initializing driver i had set implicit wait of 30 secs, so always it was taking 30 sec to timeout even though explicit wait time was 10 sec. – mk_ Aug 05 '19 at 19:49
0

As per WebDriverWait class source:

Wait will ignore instances of NotFoundException that are encountered (thrown) by default in the 'until' condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add)

And NotFoundException is a super class for the following Exceptions:

  • NoAlertPresentException
  • NoSuchContextException
  • NoSuchCookieException
  • NoSuchElementException
  • NoSuchFrameException
  • NoSuchWindowException

Therefore you will not see NoSuchElement exception when using WebDriverWait.

It might also be the case your element is actually present in the DOM but it's not visible due to having i.e. display:none CSS property so you could consider using presenceOfElementLocated condition instead.

More information: How to use Selenium to test web applications using AJAX technology

Dmitri T
  • 159,985
  • 5
  • 83
  • 133