首页 > 解决方案 > 等待多个图表加载

问题描述

我在一个仪表板中有多个图表。我想等到所有图表加载完毕。我在java中完成了这段代码。我想用python完成。请帮忙

wait.until(new ExpectedCondition<Boolean>() {

       public Boolean apply(WebDriver driver) {

           Iterator<WebElement> eleIterator = driver.findElements(By.xpath("//img[@class='loading']")).iterator();

           while (eleIterator.hasNext())                    
             {
               boolean displayed = false;
               try
               {
                   displayed = eleIterator.next().isDisplayed();     
               }

               catch (NoSuchElementException | StaleElementReferenceException e) 
               {
                   displayed = false;
               }
               if (displayed) 
               {
                   return false;

               } 
             }
           }
           return true;
       }
   });

我想要这个代码使用 python 或任何建议如何等到所有图表加载到 selenium python

标签: pythonselenium

解决方案


您可以使用 python 中的显式等待来等待所有元素可见或出现在 DOM 中。

WebDriverWait(driver,30).until(EC.visibility_of_all_elements_located((By.XPATH,"//img[@class='loading']")))

或者

WebDriverWait(driver,30)until(EC.presence_of_all_elements_located((By.XPATH,"//img[@class='loading']")))

要使用显式等待,您必须导入以下内容

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

推荐阅读