ExpectedCondition.invisibility_of_element_located 需要更多时间(selenium web driver-python)

use*_*831 4 python selenium xpath selenium-webdriver

我使用下面的ExpectedCondition方法来确保元素消失,然后我的测试继续进行

wait.until(EC.invisibility_of_element_located((By.XPATH,busyIndicator)))
Run Code Online (Sandbox Code Playgroud)

我正在做的是点击保存按钮。它将显示busyindicator 对象。保存操作完成后,忙指示符消失,表示保存操作已完成。

在这里,虽然 busyindicator 对象从 UI 中快速消失,但我上面的 webdriver 命令仍然需要将近 30-40 秒才能确保删除此元素。

需要帮助 1) 如何优化上述代码以使其快速执行 2) 确保元素消失的其他更好方法。

Luk*_*sky 6

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)))
Run Code Online (Sandbox Code Playgroud)

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.


Zac*_*ach 0

指定 webDriver 检查可见性的适当等待时间可以是 aFluentWait或 Low ExplicityWait。下面是 Java 代码片段,我认为它会让您了解如何在其他绑定上实现。希望这可以帮助

      //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"));
   } 
});
Run Code Online (Sandbox Code Playgroud)