How can I get Selenium Web Driver to wait for an element to be accessible, not just present?

后端 未结 4 1227
不思量自难忘°
不思量自难忘° 2020-11-29 23:06

I am writing tests for a web application. Some commands pull up dialog boxes that have controls that are visible, but not available for a few moments. (They are greyed out,

4条回答
  •  被撕碎了的回忆
    2020-11-29 23:55

    There are already some great answers posted up here, but I thought I would add my solution. Explicit wait etc. are great functions for use in testing with selenium. However explicit wait merely performs the function of a Thread.Sleep() that you can only set one time. The function below is what I used to "Shave off" a few minutes. It waits until the element is "accessible."

        //ALTERNATIVE FOR THREAD.SLEEP
    public static class Wait
    {  
        //public static void wait(this IWebDriver driver, List IWebElementLIst)
        public static void wait(this IWebDriver driver, By bylocator)
        {
            bool elementPresent = IsPresent.isPresent(driver, bylocator);
            while (elementPresent != true)
            {
                Thread.Sleep(1000);
    
                elementPresent = IsPresent.isPresent(driver, bylocator); 
    
            }
    
        }
    
    }
    

    It is in C#, but to adapt it would not be that difficult. Hope this helps.

提交回复
热议问题