我们是否有任何通用功能来检查页面是否已在Selenium中完全加载 [英] Do we have any generic funtion to check if page has completely loaded in Selenium

查看:233
本文介绍了我们是否有任何通用功能来检查页面是否已在Selenium中完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在selenium中检查网页是否已加载完成(即检查是否已加载所有控件)。

I am trying to check if web page is loaded completed or not (i.e. checking that all the control is loaded) in selenium.

我尝试下面的代码:

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

但即使页面加载上面代码也不会等待。

but even if page is loading above code does not wait.

我知道我可以检查特定元素以检查它是否可见/可点击等但我正在寻找一些通用解决方案

I know that I can check for particular element to check if its visible/clickable etc but I am looking for some generic solution

推荐答案

正如您所提到的,如果有任何通用功能来检查页面是否已在Selenium中完全加载,答案是

As you mentioned if there is any generic funtion to check if page has completely loaded in Selenium the answer is No.

首先让我们看看你的代码试用版如下:

First let us have a look at your code trial which is as follows :

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

上面一行代码中的参数 pageLoadTimeout 并不真正重新编码实际 pageLoadTimeout()

The parameter pageLoadTimeout in the above line of code doesn't really reseambles to actual pageLoadTimeout().

在这里,您可以找到有关 Selenium中的pageLoadTimeout无效的详细讨论

Here you can find a detailed discussion of pageLoadTimeout in Selenium not working

现在,当您的 usecase 与完全加载的页面相关时,您可以使用pageLoadStrategy()设置为 正常 [支持的值 none 渴望正常]使用eith通过 DesiredCapabilities 类或 ChromeOptions 类的实例,如下所示:

Now as your usecase relates to page being completely loaded you can use the pageLoadStrategy() set to normal [ the supported values being none, eager or normal ] using either through an instance of DesiredCapabilities Class or ChromeOptions Class as follows :


  • 使用 DesiredCapabilities 类:

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.firefox.FirefoxOptions;
 import org.openqa.selenium.remote.DesiredCapabilities;

 public class myDemo2 
 {
    public static void main(String[] args) throws Exception 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        DesiredCapabilities dcap = new DesiredCapabilities();
        dcap.setCapability("pageLoadStrategy", "normal");
        FirefoxOptions opt = new FirefoxOptions();
        opt.merge(dcap);
        WebDriver driver = new FirefoxDriver(opt);
        driver.get("https://www.google.com/");
        System.out.println(driver.getTitle());
        driver.quit();
    }
}


  • 使用 ChromeOptions 类:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.PageLoadStrategy;
    
    public class myDemo1 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            FirefoxOptions opt = new FirefoxOptions();
            opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
            WebDriver driver = new FirefoxDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    


  • 您可以在 Chrome驱动程序的网页加载策略(已更新至Selenium v​​3.12.0)

    现在将 PageLoadStrategy 设置为 正常 并且您的代码试用版都确保浏览器客户端具有(即 Web浏览器)已达到'document.readyState'等于完成。一旦满足此条件,Selenium将执行下一行代码。

    Now setting PageLoadStrategy to NORMAL and your code trial both ensures that the Browser Client have (i.e. the Web Browser) have attained 'document.readyState' equal to "complete". Once this condition is fulfilled Selenium performs the next line of code.

    您可以在 Selenium IE WebDriver仅在调试时有效

    浏览器客户端获得'document.readyState'等于complete仍然不能保证所有 JavaScript Ajax Calls 都已完成。

    But the Browser Client attaining 'document.readyState' equal to "complete" still doesn't guarantees that all the JavaScript and Ajax Calls have completed.

    要等待所有 JavaScript Ajax Calls 完成,你可以写一个功能如下:

    To wait for the all the JavaScript and Ajax Calls to complete you can write a function as follows :

    public void WaitForAjax2Complete() throws InterruptedException
    {
        while (true)
        {
            if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){
                break;
            }
            Thread.sleep(100);
        }
    }
    

    您可以在等待ajax请求完成 - selenium webdriver

    现在,上述两种方法通过 PageLoadStrategy 返回jQuery.active == 0看起来正在等待无限期事件。因此,对于明确的等待,你可以诱导 WebDriverWait ExpectedConditions 设置为 titleContains () 方法将确保页面标题(即网页)可见并假设所有元素也可见如下:

    Now, the above two approaches through PageLoadStrategy and "return jQuery.active == 0" looks to be waiting for indefinite events. So for a definite wait you can induce WebDriverWait inconjunction with ExpectedConditions set to titleContains() method which will ensure that the Page Title (i.e. the Web Page) is visible and assume the all the elements are also visible as follows :

    driver.get("https://www.google.com/");
    new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partial_title_of_application_under_test"));
    System.out.println(driver.getTitle());
    driver.quit();
    






    现在,有时可以通过页面标题将匹配您的应用程序标题仍然是您想要交互的所需元素尚未完成加载。所以更细粒度的方法是诱导 WebDriverWait ExpectedConditions 设置为 visibilityOfElementLocated() 方法,它将使您的程序等待所需的元素可见,如下所示:


    Now, at times it is possible though the Page Title will match your Application Title still the desired element you want to interact haven't completed loading. So a more granular approach would be to induce WebDriverWait inconjunction with ExpectedConditions set to visibilityOfElementLocated() method which will make your program wait for the desired element to be visible as follows :

    driver.get("https://www.google.com/");
    WebElement ele = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_of_the_desired_element")));
    System.out.println(ele.getText());
    driver.quit();
    

    这篇关于我们是否有任何通用功能来检查页面是否已在Selenium中完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆