Selenium C# WebDriver: Wait until element is present

前端 未结 24 3226
礼貌的吻别
礼貌的吻别 2020-11-22 08:53

I want to make sure that an element is present before the webdriver starts doing stuff.

I\'m trying to get something like this to work:

WebDriverWait w         


        
24条回答
  •  感动是毒
    2020-11-22 09:49

    I see multiple solutions already posted that work great! However, just in case anyone needs something else, I thought I would post two solutions that I personally used in selenium C# to test if an element is present! Hope it helps, cheers!

    public static class IsPresent
    {
        public static bool isPresent(this IWebDriver driver, By bylocator)
        {
    
            bool variable = false;
            try
            {
                IWebElement element = driver.FindElement(bylocator);
                variable = element != null;
            }
           catch (NoSuchElementException){
    
           }
            return variable; 
        }
    
    }
    

    Here is the second

        public static class IsPresent2
    {
        public static bool isPresent2(this IWebDriver driver, By bylocator)
        {
            bool variable = true; 
            try
            {
                IWebElement element = driver.FindElement(bylocator);
    
            }
            catch (NoSuchElementException)
            {
                variable = false; 
            }
            return variable; 
        }
    
    }
    

提交回复
热议问题