1

I am using Selenium 2.25 WebDriver

I'm having a issue with finding the elements on the page and some times my test cases able to find element and sometime the page is does not load and its due to page load and if i add this below line and it seems like working:

 driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2));

my question is, i dont want to have my code scatter with the above line of code, is there a way to make it centerlize in one place?

Any help would be greatly appreciated, thanks!

user2771704
  • 5,994
  • 6
  • 37
  • 38
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

2 Answers2

2

If you set the timeout once, it's set for the lifetime of the driver instance. You don't need to keep resetting it. You can set this immediately after creating the driver.

IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts.SetPageLoadTimeout(TimeSpan.FromSeconds(2));

The only caveat for using this timeout is that not every browser may support it completely (IE does for sure, Firefox does too I think, but I don't think Chrome does).

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Thanks Jim, where should I put that code? in order for the set timeout for the lifetime of the driver instance? – Nick Kahn Jan 30 '13 at 15:03
0

You can try a workaround like this:

Observe the element that loads last in your page and find its id (or any other identifier). Then do something like this:

 while (true)
        {
            try
            {   
                IWebElement element = driver.FindElement(By.Id(...));
                if (element.Displayed)
                {
                    break;
                }
            }
            catch (Exception)
            {
                continue;
            }
        }

This will keep looping till the element which is loaded last is displayed and breaks thereupon. The element not found exception is caught and loop is put into continuation till the element is not displayed.

Manya
  • 315
  • 4
  • 17