【问题标题】:Waiting for get requests to finish in selenium等待 get 请求在 selenium 中完成
【发布时间】:2018-09-11 13:43:13
【问题描述】:

对于这个例子,让我们看看网站 pinterest:

当我进行初始登录时,会加载一些图钉。 为了获得更多图钉,我需要滚动到页面末尾,在请求更多图钉之后(我想很多网站的工作方式都是这样)

所以我知道如何在 selenium 中进行滚动,但是我如何等待请求结束?

我的意思是,它不是在等待某个元素出现,那种元素(大头针)已经存在,但我正在等待其他元素出现。

如果我使用带有等待的预期条件,这对第一批引脚有好处,但是那些添加到它们的引脚,我如何等待它们,例如:

当 pinterest 首次加载时->

WebDriverWait driverWait = new WebDriverWait(cd, 10, 1000);

element = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("thepins")));

这对于初始加载非常有用,现在我滚动到页面底部

((JavascriptExecutor) cd).executeScript("window.scrollTo(0, document.body.scrollHeight)");

现在取决于页面,加载到更多引脚(有时不是)我想等待它们加载,然后再进行另一个滚动。

解决这种情况的最佳方法是什么?

【问题讨论】:

    标签: java ajax selenium selenium-webdriver get


    【解决方案1】:

    我这样做的方法是使用页面上的元素数量。所以例如你可以这样做:

    int pinCount = webDriver.findElements(By.xpath("thepins")).size();
    

    此时为滚动运行你的 jsExecutor,然后:

    webDriverWait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("thepins"),pinCount ));
    

    【讨论】:

    • 嘿,你太快了 :-)
    • 所以这里有一种情况,当我向下滚动并执行我提到的 xpath 时,以前的一些引脚丢失了它们的引用,这意味着如果我向下滚动到页面底部页面中的负载和引脚,我看到引用了 23 而不是例如 200...
    【解决方案2】:

    我认为处理这个问题的最好方法是让课程拥有所有的可能性,而不是从需要等待的地方调用它,下面的代码会发挥作用,对我来说效果很好

    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Waiter {
    
        private static WebDriver jsWaitDriver;
        private static WebDriverWait jsWait;
        private static JavascriptExecutor jsExec;
    
        //Get the driver 
        public static void setDriver (WebDriver driver) {
            jsWaitDriver = driver;
            jsWait = new WebDriverWait(jsWaitDriver, 45);
            jsExec = (JavascriptExecutor) jsWaitDriver;
        }
    
        //Wait for JQuery Load
        public static void waitForJQueryLoad() {
            //Wait for jQuery to load
            ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver)
                    .executeScript("return jQuery.active") == 0);
    
            //Get JQuery is Ready
            boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0");
    
            //Wait JQuery until it is Ready!
            if(!jqueryReady) {
                System.out.println("JQuery is NOT Ready!");
                //Wait for jQuery to load
                jsWait.until(jQueryLoad);
            } else {
                System.out.println("JQuery is Ready!");
            }
        }
    
        //Wait for Angular Load
        public static void waitForAngularLoad() {
            WebDriverWait wait = new WebDriverWait(jsWaitDriver,45);
            JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
    
            String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";
    
            //Wait for ANGULAR to load
            ExpectedCondition<Boolean> angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver)
                    .executeScript(angularReadyScript).toString());
    
            //Get Angular is Ready
            boolean angularReady = Boolean.valueOf(jsExec.executeScript(angularReadyScript).toString());
    
            //Wait ANGULAR until it is Ready!
            if(!angularReady) {
                System.out.println("ANGULAR is NOT Ready!");
                //Wait for Angular to load
                wait.until(angularLoad);
            } else {
                System.out.println("ANGULAR is Ready!");
            }
        }
    
        //Wait Until JS Ready
        public static void waitUntilJSReady() {
            WebDriverWait wait = new WebDriverWait(jsWaitDriver,45);
            JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
    
            //Wait for Javascript to load
            ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) jsWaitDriver)
                    .executeScript("return document.readyState").toString().equals("complete");
    
            //Get JS is Ready
            boolean jsReady =  (Boolean) jsExec.executeScript("return document.readyState").toString().equals("complete");
    
            //Wait Javascript until it is Ready!
            if(!jsReady) {
                System.out.println("JS in NOT Ready!");
                //Wait for Javascript to load
                wait.until(jsLoad);
            } else {
                System.out.println("JS is Ready!");
            }
        }
    
        //Wait Until JQuery and JS Ready
        public static void waitUntilJQueryReady() {
            JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
    
            //First check that JQuery is defined on the page. If it is, then wait AJAX
            Boolean jQueryDefined = (Boolean) jsExec.executeScript("return typeof jQuery != 'undefined'");
            if (jQueryDefined == true) {
                //Pre Wait for stability (Optional)
                sleep(30);
    
                //Wait JQuery Load
                waitForJQueryLoad();
    
                //Wait JS Load
                waitUntilJSReady();
    
                //Post Wait for stability (Optional)
                sleep(30);
            }  else {
                System.out.println("jQuery is not defined on this site!");
            }
        }
    
        //Wait Until Angular and JS Ready
        public static void waitUntilAngularReady() {
            JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver;
    
            //First check that ANGULAR is defined on the page. If it is, then wait ANGULAR
            Boolean angularUnDefined = (Boolean) jsExec.executeScript("return window.angular === undefined");
            if (!angularUnDefined) {
                Boolean angularInjectorUnDefined = (Boolean) jsExec.executeScript("return angular.element(document).injector() === undefined");
                if(!angularInjectorUnDefined) {
                    //Pre Wait for stability (Optional)
                    sleep(30);
    
                    //Wait Angular Load
                    waitForAngularLoad();
    
                    //Wait JS Load
                    waitUntilJSReady();
    
                    //Post Wait for stability (Optional)
                    sleep(30);
                } else {
                    System.out.println("Angular injector is not defined on this site!");
                }
            }  else {
                System.out.println("Angular is not defined on this site!");
            }
        }
    
        //Wait Until JQuery Angular and JS is ready
        public static void waitJQueryAngular() {
            waitUntilJQueryReady();
            waitUntilAngularReady();
        }
    
        public static void sleep (Integer seconds) {
            long secondsLong = (long) seconds;
            try {
                Thread.sleep(secondsLong);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果您知道总共会显示多少个图钉,您可以使用:

      // new wait 30 seconds
      WebDriverWait wait30s = new WebDriverWait(driver,30);
      wait30s.until(ExpectedConditions.numberOfElementsToBe("THIS LOCATOR MUST FIT TO ALL PINS", number));
      
      // or
      wait30s.until(ExpectedConditions.numberOfElementsToBeMoreThan("THIS LOCATOR MUST FIT TO ALL PINS", number));
      

      【讨论】:

        猜你喜欢
        • 2018-08-20
        • 2018-11-28
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-04
        相关资源
        最近更新 更多