[Selenium]怎样等待元素出现之后再消失,譬如Loading icon

界面上有些元素是要先等它出现,再等它消失,譬如loading icon

这个是等多个loading icon出现后消失

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
     * Wait for loading icon disappear in dialog and Widgets
     * @author jz
     */
    public void waitForLoadingIconDisappearInDialog(WebDriver driver) {
        System.out.println("Wait for loading icons display in dialog or widgets.");
        Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                try {
                    for (WebElement el : driver.findElements(By.cssSelector("div.win-bd-mask[style*='display: block'] div.win-bd-mask-waiting"))) {
                        if (el.isDisplayed()) {
                            System.out.println("Loading icon display in dialog or widgets.");
                            return false;
                        }
                    }
                }
                catch (Exception ex) {
                    return true;
                }
                return true;
            }
        };
 
        WebDriverWait wait = new WebDriverWait(driver, 120, 1000);
        wait.withMessage("Loading icons should disppear in dialog or widgets in 120s");
        wait.until(waitFn);
        System.out.println("Loading icons disspear in dialog or widgets.");
    }

 这个是等一个loading icon 出现后消失

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void waitForLoadingIconDisappearInNewWebPage(WebDriver driver) {
        System.out.println("Wait for loading icon display in new web page.");
        final By cssSelector=By.cssSelector("div.x-mask-msg");
        Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
            public Boolean apply(WebDriver driver) {
                try{
                    if(driver.findElement(cssSelector).isDisplayed()){
                        System.out.println("Loading icon display in new web page.");
                        return false;
                    }
                }
                catch(Exception e){
                    return true;
                }
                return true;
            }
        };
        WebDriverWait wait = new WebDriverWait(driver, 120, 2000);
        wait.withMessage("Loading icon should disappear in new web page in 120s");
        wait.until(waitFn);
        System.out.println("Loading icon disappear in new web page.");
    }

 如果等显现的元素变成隐藏,相对要简单一些,先等它可见,再等它不可见

1
2
3
4
public void waitForLoadingIconDisappearInHomePage(WebDriver driver) {
        this.waitForElementVisible(driver, By.cssSelector("div#window-waiting-box"), 20 ,"Waiting box should display in 20s");
        this.waitForElementNotVisible(driver, By.cssSelector("div#window-waiting-box"), 120 ,"Waiting box should disppear in 120s");
    }

 

posted on   张缤分  阅读(2399)  评论(1编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示