Reputation: 47
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 ...!!!
Upvotes: 1
Views: 2898
Reputation: 168002
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:
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
Upvotes: 0
Reputation: 193088
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
.
Upvotes: 1