user3484831
user3484831

Reputation: 107

ExpectedCondition.invisibility_of_element_located takes more time (selenium web driver-python)

I am using below ExpectedCondition method to ensure that element disappears and my test proceeds after that

wait.until(EC.invisibility_of_element_located((By.XPATH,busyIndicator)))

What I am doing is click on save button. It will show busyindicator object. Once Save operation is done busy indicator disappears to indicate save operation is done.

Here, though busyindicator object disappears quickly from UI, but still my above webdriver command takes almost 30-40 seconds to ensure this element is removed.

Need help on 1) How to optimize above code so that it gets executed quickly 2) Other better way to ensure elements disappears.

Upvotes: 6

Views: 19740

Answers (3)

Murathan Aktimur
Murathan Aktimur

Reputation: 21

You should set implicitly_wait to 1 second before calling the code. I recommend you to create a new function to use this specific implicitly_wait there.

     def wait_for_spinner(driver):
        driver.implicitly_wait(1)
        wait.until(EC.invisibility_of_element_located((By.XPATH,busyIndicator)))

Upvotes: 0

Lukas Cenovsky
Lukas Cenovsky

Reputation: 5670

Make sure you don't have set up implicit timeout or set it to 0 before waiting:

driver.implicitly_wait(0)
WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.XPATH,busyIndicator)))

Implicit timeout applies to any command, that is also to _find_element used by WebDriverWait.

In your case, busy indicator is probably removed from DOM. Explicit wait for invisibility calls find_element. As the element is not in DOM, it takes implicit timeout to complete. If you have implicit wait 30s, it will take 30s. And only then explicit wait finishes successfully. Add time for which the busy indicator is visible and you are on your 30-40 seconds.

Upvotes: 9

Zach
Zach

Reputation: 1006

Specifying a decent wait time that webDriver checks for the visibility can be a FluentWait or Low ExplicityWait. Below are Java Snippet I reckon it will give you an idea on how to implement on other bindings. hope this helps

      //Specify any less time
      WebDriverWait wait = new WebDriverWait(driver, 5);
      WebElement element = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id('id')));

      //FluentWait
      Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
     .withTimeout(10, SECONDS)
     .pollingEvery(5, SECONDS)
     .ignoring(NoSuchElementException.class);

      WebElement element = wait.until(new Function<WebDriver, WebElement>() {
      public WebElement apply(WebDriver driver) {
      return driver.findElement(By.id("someid"));
   } 
});

Upvotes: 0

Related Questions