//操作滚动条到指定元素
    static void scrollbar(By by) {
        boolean Location = false;
        int sleep_total = 0;
        while ((!Location) && (sleep_total <= max_sleep_total)) {
            try {
                WebElement element = driver.findElement(by);
                JavascriptExecutor je = (JavascriptExecutor) driver;
                je.executeScript("arguments[0].scrollIntoView(true);", element);// 参数为true时调用该函数,页面(或容器)发生滚动,使element的顶部与视图(容器)顶部对齐;参数为false时,使element的底部与视图(容器)底部对齐。
                Location = true;
                System.out.println("操作滚动条成功:" + by);
            } catch (Exception e) {
                System.out.println("操作滚动条失败" + by);
                sleep(2000);
                sleep_total += 2;
                System.out.println("总共已等待 " + sleep_total + " 秒");
            }
        }
        if (!Location) {
            RunExcelCase.judge = "fail";
            System.out.println("最终操作滚动条失败" + by);
            snapshot("最终操作滚动条失败");
        }
    }

    //操作滚动条到最底部
    static void scrollbarBelow(String string) {
        boolean Location = false;
        int sleep_total = 0;
        while ((!Location) && (sleep_total <= max_sleep_total)) {
            try {
                sleep(2000);
                JavascriptExecutor je = (JavascriptExecutor) driver;
                je.executeScript("window.scrollTo(0,document.body.scrollHeight)");// 参数为true时调用该函数,页面(或容器)发生滚动,使element的顶部与视图(容器)顶部对齐;参数为false时,使element的底部与视图(容器)底部对齐。
                Location = true;
                System.out.println("操作滚动条成功:" + string);
            } catch (Exception e) {
                System.out.println("操作滚动条失败" + string);
                sleep(2000);
                sleep_total += 2;
                System.out.println("总共已等待 " + sleep_total + " 秒");
            }
        }
        if (!Location) {
            RunExcelCase.judge = "fail";
            System.out.println("最终操作滚动条失败" + string);
            snapshot("最终操作滚动条失败");
        }
    }

 

更多推荐