Selenium - Wait until element is NOT visible

后端 未结 10 2075
生来不讨喜
生来不讨喜 2021-02-02 07:29

In the code below, I attempt to wait until an element is visible:

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.Until(ExpectedCon         


        
相关标签:
10条回答
  • 2021-02-02 08:17

    I know this is old, but since I was searching for a solution to this, I thought I'd add my thoughts.

    The answer given above should work if you set the IgnoreExceptionTypes property:

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
    wait.IgnoreExceptionTypes = new[] { typeof(NoSuchElementException) }
    wait Until(driver => !driver.FindElement(By.Id("processing")).Displayed);
    
    0 讨论(0)
  • 2021-02-02 08:18
    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("processing")));
    

    The idea is to wait until element is not visible. First line sets wait time that element has to disappear; here it's 10 seconds. Second line uses selenium to check if condition "invisibilityofElementLocated" is met. Element is found by its id as in topic case, that is id="processing". If element doesn't disappear in the requested period of time, a Timeout exception will be raised and the test will fail.

    0 讨论(0)
  • 2021-02-02 08:18

    In the code below which is used to stop the driver for couple of seconds

    System.Threading.Thread.Sleep(20000);

    0 讨论(0)
  • 2021-02-02 08:23

    Use invisibility method, and here is an example usage.

    final public static boolean waitForElToBeRemove(WebDriver driver, final By by) {
        try {
            driver.manage().timeouts()
                    .implicitlyWait(0, TimeUnit.SECONDS);
    
            WebDriverWait wait = new WebDriverWait(UITestBase.driver,
                    DEFAULT_TIMEOUT);
    
            boolean present = wait
                    .ignoring(StaleElementReferenceException.class)
                    .ignoring(NoSuchElementException.class)
                    .until(ExpectedConditions.invisibilityOfElementLocated(by));
    
            return present;
        } catch (Exception e) {
            return false;
        } finally {
            driver.manage().timeouts()
                    .implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        }
    }
    
    0 讨论(0)
提交回复
热议问题