I've got Tests written using Selenium/Java. There is an element with an id="xyz". I need to wait for the above element to disappear, so I am using invisibilityOf()

WebDriverWait wait = new WebDriverWait(driver, 20);
driver.findElement(By.id("abc)).click(); //error, element xyz intercepted click

The Element just doesn't disappear. Are there any other preferred ways of waitting for an Element to disappear. I tried using JavascriptExecutor, but that would mean implementing JSExecutor for every interaction on the webpage. The big Problem is that the result is flakky, sometimes it works and sometimes not with the above code.

  • You should first validate that the element is visible and then validate invisible
    – PDHide
    Feb 24 '20 at 13:43
  • If doesn't solve your issue please comment more details
    – PDHide
    Feb 24 '20 at 13:43
  • But the Problem isn't that the Element is not being found or anything. The Problem comes with the next step that even thogh I validated it is invisible, it shows back up. It's as if I need to validated before each interaction on the webpage that the Element xyz is invisible and even then I can't be sure that it's invisible because it still randomly throws this error. Feb 25 '20 at 7:19
  • This is because you are validating the element invisibility before it appears, so say you want to open an alert that comes up and disappears in 2sec and you check only for its invisibility. So, if there was a delay in opening the alert, your test condition of the element invisible will be a success as the alert is not opened yet and will continue your step, but by that time, alert will appear and your test fails. So first check that the lement appears and then disappears , to avoid this issue
    – PDHide
    Feb 25 '20 at 7:32