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!

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jim, where should I put that code? in order for the set timeout for the lifetime of the driver instance?
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.

1 Comment

I don't think having a forced while loop will benefit test perfomance

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.