Page Load Timeout - Selenium Webdriver using C#

谁说我不能喝 提交于 2019-12-18 18:22:35

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/14606675/page-load-timeout-selenium-webdriver-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!