Search code examples
pythonseleniumexpected-condition

What is the difference between some similar selenium waits?


Simple question:

What is the exact difference between those two statements:

  1. WebDriverWait(self._driver, WEB_WAIT_TIMEOUT).until(ec.invisibility_of_element_located(element))

and

  1. WebDriverWait(self._driver, WEB_WAIT_TIMEOUT).until_not(ec.presence_of_element_located(element))

In both cases, selenium behavior is the same in my situation. Thanks in advance

Thanks for responses Ok but there are still things I dont understand: I've got basic function that checks if spinner is not visible.

`def wait_until_request_api_process_finished(self):
    try:
        WebDriverWait(self._driver, 1).until(ec.visibility_of_element_located(BaseLoc.spinner))
        WebDriverWait(self._driver, 10).until(ec.invisibility_of_element_located(BaseLoc.spinner))
    except TimeoutException:
        pass

But, even if the spinner is not visible, selenium waits (about 8 seconds more than expected). What's the issue?


Solution

  • I have got few things from Selenium official docs : here

    Warning:

    Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

    so for your this question :

    But, even if the spinner is not visible, selenium waits (about 8 seconds more than expected). What's the issue?

    • yes it is because you mixed both of them. Before removing implicit wait (think 8 sec really matters or more reliability of your test suite matters)

    Now for this question :

      invisibility_of_element_located 
    

    basically see this internal implementation :

     private static boolean isInvisible(final WebElement element) {
        try {
          return !element.isDisplayed();
        } catch (StaleElementReferenceException ignored) {
          // We can assume a stale element isn't displayed.
          return true;
        }
      }
    

    so it is just checking for isDisplayed() internally.