1

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?

maxt026
  • 63
  • 10
  • Well what is the difference between `invisibility` (did you mean `visibility_of_element_located`) and `presence`? – Guy Jun 30 '21 at 12:05
  • Indeed, didn't notice any differences between – maxt026 Jun 30 '21 at 12:07
  • Forget Selenium, read the name of the functions in English. There might not be a difference in you application, but the do different things. – Guy Jun 30 '21 at 12:09
  • 1
    ut, even if the spinner is not visible, selenium waits (about 8 seconds more than expected). What's the issue? - Do you have implicit waits also in your code ? – cruisepandey Jun 30 '21 at 12:32
  • Yes, I've got implicit wait set to 10 sec. Should I remove it or rather chenge it? – maxt026 Jun 30 '21 at 12:34
  • @cruisepandey The issue was that I had implicit wait that prolonged waiting for the spinner to be invisible. Thank you mate! – maxt026 Jun 30 '21 at 12:40
  • @maxt026 : yes. Mixing them would cause a unpredictable wait times. – cruisepandey Jun 30 '21 at 12:43

4 Answers4

1

ec.invisibility_of_element_located waits for element invisibility.
Element can be existing on the page but be invisible.
For such element ec.invisibility_of_element_located will successfully return True and your scenario flow will continue while until_not(ec.presence_of_element_located(element)) will throw timeout exception since the element is still existing on the page.

Prophet
  • 32,350
  • 22
  • 54
  • 79
1

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

The top statement waits until (ec.invisibility_of_element_located(element)) happens were as the bottom would wait until (ec.presence_of_element_located(element)) does not happen.

Side note functionally I know that doesn't make sense but that's what is being asked :)

0

The answer to the second (added) part of the question:

It is possible (and probable) that the selenium script checks for invisibility before the page has finished loading. Selenium has no way of knowing if the element was visible before and now is not present in the DOM, that's why it will first wait for visibility and then invisibility.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77