Selenium C# WebDriver: Wait until element is present

前端 未结 24 2856
礼貌的吻别
礼貌的吻别 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:48

    Python:

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    
    driver.find_element_by_id('someId').click()
    
    WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, 'someAnotherId'))
    

    from EC you can choose other conditions as well try this: http://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.support.expected_conditions

    0 讨论(0)
  • 2020-11-22 09:48

    You can use the following

    WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
    wait.Until(ExpectedConditions.ElementToBeClickable((By.Id("login")));
    
    0 讨论(0)
  • 2020-11-22 09:49

    Try this code:

     New WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(Function(d) d.FindElement(By.Id("controlName")).Displayed)
    
    0 讨论(0)
  • 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; 
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:50

    This is the reusable function to wait for an element present in DOM using Explicit Wait.

    public void WaitForElement(IWebElement element, int timeout = 2)
    {
        WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromMinutes(timeout));
        wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
        wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
        wait.Until<bool>(driver =>
        {
            try
            {
                return element.Displayed;
            }
            catch (Exception)
            {
                return false;
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 09:50

    Was searching how to wait in Selenium for condition, landed in this thread and here is what I use now:

        WebDriverWait wait = new WebDriverWait(m_driver, TimeSpan.FromSeconds(10));
        wait.Until(d => ReadCell(row, col) != "");
    

    ReadCell(row, col) != "" can be any condition. Like this way because:

    • it's mine
    • allows inlining
    0 讨论(0)
提交回复
热议问题