Selenium2/WebDriver is optimized for positive testing paths. My web application hides elements based on user permissions. I am looking for an element and throwing-catching the exception when it does not find it and the command fails (I'm using Java). This verifies the element is not present. I've seen this question: Quicker way to assert that an element does not exist but I am wondering if there is an alternative way besides if/else statements.

Edited: The code currently being used is shown below. This currently takes ~60+ seconds to run.

@User246: I guess my main question is there a parameter or something like that to pass to Selenium that tells it to only look for the WebElement for X seconds before throwing the exception. Optimally I would set it to 1 second since I expect it to not be found and just want to catch the exception. I hope this is more clear.

protected void assertAddButtonIsHidden() throws Exception
    boolean found = true;

        WebElement addButton = findElementById("add");
    catch (StaleElementReferenceException x)
        found = false;

  • I do not understand the question. You seem to object to using Exceptions. That leaves calling a function that returns a value, but of course something will need to behave differently depending on that value. In Java, we use if-statements for that. You could use a switch statement instead, but surely an if-statement would be easier. Perhaps you could add a code fragment that describes what you are trying to do.
    – user246
    Commented Sep 20, 2012 at 19:37
  • I added the code and updated the question. Hopefully it is a little clearer. I am a little long-winded at times.
    – squeemish
    Commented Sep 20, 2012 at 19:47
  • When you say the code takes 60 seconds to run, do you mean calling assertAddButtonIsHidden just once takes 60 seconds?
    – user246
    Commented Sep 20, 2012 at 20:44
  • That is correct. The entire snippet up there runs for ~60+ seconds, hanging while trying to find the element that is not present
    – squeemish
    Commented Sep 20, 2012 at 20:48

2 Answers 2


I have not used the java implimentation but can you utilize the ImplicityWait method to configure the timeout?

This is a c# example that gets set in the Setup stage of the unit test.

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0,30));

This is the API doc for the method.

  • Surprisingly, this worked great. I was under the impression from looking at the API doc previously that it could only be used for EXTENDING the time it looked, but I set it to 150 milliseconds and now a test that took 60 seconds takes 7. Thanks a lot!
    – squeemish
    Commented Sep 21, 2012 at 14:35

if u are using testng framework the and test time out

  • 1
    What does this add to the existing accepted answer?
    – Kate Paulk
    Commented Oct 5, 2015 at 11:37
  • 1
    This is a little different than the accepted answer. It actually seems better because it implies it's a property of the test itself as opposed to an action performed by the test. That said, the answer should make that clear. Answers that are only good for people familiar with the answer are inferior to answers that explain themselves. =)
    – corsiKa
    Commented Oct 5, 2015 at 19:32
  • @Kate Paulk i think u r happy with corsiKa answer, do i need to add something? Commented Oct 6, 2015 at 5:03
  • @sameerjoshi - could you edit your answer to say that using a time out as a property of the test may be better than having the time out as an action performed by the test? That way when comments are cleared - which happens occasionally - all the information is in the answer.
    – Kate Paulk
    Commented Oct 6, 2015 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.