Maya
Maya

Reputation: 7203

How to handle Selenium timeout effectively?

Selenium.WaitForPageLoad("50000"); 
Selenium.Click("title");

Most times when it takes more than 50000, I get Timeout error. I do not want to increase the time. Is there a way to give it something like "take as long as you want"?

Upvotes: 1

Views: 9463

Answers (4)

Sabarish
Sabarish

Reputation: 912

This works for me

   /**
    * Put the Thread to Sleep for the specific time
    * @param delay
    */
   private void sleep(long time) {
      try {
         Thread.sleep(time);
      } catch (InterruptedException e) {
      }
   }

   //Inside my Test Method

   //check if the element is loaded
   while (!webElement.isLoaded) {
        //sleep for a sec
         sleep(1000);
      }

Upvotes: 0

faramka
faramka

Reputation: 2234

Have you tried to reverse the order of these commands?

Selenium.Click("title");
Selenium.WaitForPageToLoad("50000"); 

Or simply use:

Selenium.ClickAndWait("title");

Upvotes: 0

prestomanifesto
prestomanifesto

Reputation: 12796

I don't recommend it, but if you want selenium to wait indefinitely, use a try-catch block and a loop:

while (true)
{
   try
   {
       Selenium.WaitForPageLoad("5000");
       break; //executed if page finished loading
   }
   catch (Exception)
   {
       //ignore the timeout exception
   }
}

Again this is probably a bad idea, timeouts are generally a good thing. Depending on what you are trying to do you might want to consider checking if a particular element has finished loading as opposed to the whole page.

Upvotes: 2

peter.swallow
peter.swallow

Reputation: 945

Are you sure you want it to take as long as it takes? It could hang all day...

I generally found if it takes ages to run, it had already failed a long time before.

I have used WatiN before and you can use a .WaitUntil() command to check that a specific element on the page has been loaded. Not sure what the equivalent in Selenium would be.

This link may help, if you're happy creating add-ins:

http://release.seleniumhq.org/selenium-remote-control/0.9.0/doc/java/com/thoughtworks/selenium/Wait.html

Upvotes: 1

Related Questions