I am pretty new to all the Automated Testing thing and recently I have been having trouble with selenium because sometimes fails finding certain input fields / buttons.

I added explicit waits for every element in the page, so the test is supposed to wait for that element to appear on screen, but sometimes randomly fails to do so. I will attach the latest error thrown by selenium in a simple test.

This is one of the tests that I am trying to run and randomly fails:

public void createTaskTest(){
    this.newtask.setType("Personal Appointment");
    this.newtask.setPaymentType("End of the day");

Error thrown by selenium:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: span.dateOnlyInput>input (tried for 130 second(s) with 500 MILLISECONDS interval)

Here is the method to find the 'missing' date input:

public void setDateField(String date){
    WebDriverWait wait = new WebDriverWait(this.driver,130);

3 Answers 3


Best option can be write your code inside a try catch block. Then if there are any error (which exception contains time out) , then simply run the same method inside the try catch block with a wait or sleep command which will increase the waiting time for the element..

If the time out error is a never stopping error you will be ended up with a never ending loop. But if you use a for loop with a count you can avoid being inside a never ending loop.

  • I do suggest to use - wait.until instead of try & catch before action. Dont you think it would be very difficult to maintain? Commented Dec 21, 2016 at 13:03
  • I already used wait.until and gave it enough time to search for the element, but it seems that is not enough. The try catch thing solved my problem for now Commented Dec 21, 2016 at 14:11

If it fails randomly, then there might be a lot of things going on there.

Catch the exception and dump the source of the page at the moment of the exception. Then analyze it and check whether you can find the input element. If it's there, make sure your CSS path is correct (maybe the HTML tree is not always the same?).

If it's not there, maybe you need to increase the timeout because the page is so slow.

  • The html is always the same so that cant be it. Another thing to notice is that all the tests work well isolated, but when running the 4 tests of the class random TimeoutExceptions appear sometimes. Maybe i need to do something in between tests? Thanks for the help Commented Dec 20, 2016 at 17:01
  • Which browser are you running the tests on? I've experienced random issues like that with the IE driver in the past.
    – Radek
    Commented Dec 20, 2016 at 19:48
  • Im using Firefox in this test Commented Dec 20, 2016 at 20:22

Few fails are really random, Observed same. Below few solutions might help:

  1. Change in wait function: Max. its working well for me

  2. Try with different locators:

    • ID

    • Name

    • CSS

    • LinkText

    • Then combinations of above like ID & CSS, ID & name etc

  • How do you try combinations of id css and name? thank you for the help Commented Dec 21, 2016 at 11:27
  • You should use visibilityOfElementLocated not visibilityOf as the last method does not check if the element is available in the DOM. Commented May 26, 2020 at 13:11

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.