如何使用Selenium WebDriver获取页面加载的精确时间?

9

如何使用Selenium WebDriver获取页面加载的精确时间?

我们可以使用Thread.sleep。

我们可以使用implicitlyWait。

我们可以使用WebDriverWait。

但是,如何使用Selenium WebDriver获取页面加载的精确时间呢?


你想使用哪种编程语言? - Ripon Al Wasim
使用导航计时 API - Corey Goldberg
除了@corey-goldberg的评论之外:Python示例https://gist.github.com/mkaz/3047779和文档https://www.w3.org/TR/navigation-timing/#processing-model - Alexey Shrub
5个回答

18
如果您想要使用 Selenium WebDriver(也称为 Selenium 2)来确定完全加载一个页面需要多长时间,通常情况下 WebDriver 应该在页面加载完成后才将控制权返回给您的代码。因此,以下 Selenium Java 代码可能会对您有所帮助:
long start = System.currentTimeMillis();

driver.get("Some url");

long finish = System.currentTimeMillis();
long totalTime = finish - start; 
System.out.println("Total Time for page load - "+totalTime); 
如果这样不起作用,那么你必须等到页面上显示出某个元素。
 long start = System.currentTimeMillis();

driver.get("Some url");

WebElement ele = driver.findElement(By.id("ID of some element on the page which will load"));
long finish = System.currentTimeMillis();
long totalTime = finish - start; 
System.out.println("Total Time for page load - "+totalTime); 

1
唯一可能的问题是您还包括发送/接收Selenium命令所需的时间。如果您在Web上使用中心,则此额外的延迟可能会使页面加载时间结果产生偏差。 - Jochen
1
是的,你肯定是对的。这不是完全正确的答案。如果我们不使用Java和Selenium,一个附加组件可能会给我们正确的时间 - http://www.searchenginejournal.com/best-firefox-addons-to-analyze-the-page-load-time/12419/。请告诉我是否有任何方法可以使用Java代码实现页面加载时间。 - Hari Reddy
1
如果Ajax调用正在进行,而元素也是可见的呢? 示例网站 - http://www.mortgage-lender-reviews.com/reviews/amerisave-reviews.html - Ishank

5

您可以使用org.apache.commons.lang3.time包中的StopWatch对象。以下是使用Java的Selenium WebDriver的完整代码:

import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TimerInSeleniumWebDriver {
    public static void main(String[] args) {
        WebDriver driver;
        driver = new FirefoxDriver();       
        StopWatch pageLoad = new StopWatch();
        pageLoad.start();
        //Open your web app (In my case, I opened facebook)
        driver.get("https://www.facebook.com/");
        // Wait for the required any element (I am waiting for Login button in fb)
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l")));

        pageLoad.stop();
        //Get the time
        long pageLoadTime_ms = pageLoad.getTime();
        long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
        System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
        System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
        driver.close();
    }
}

0

为了获得最佳体验,请使用TestListners:

@AfterMethod
    public void getResult(ITestResult result) throws Exception {
        String sMethodName = "";
        Long lExecutionTime;
        sMethodName = result.getMethod().getMethodName().toString();
        lExecutionTime = (result.getEndMillis() - result.getStartMillis()) / 1000;
        System.out.println(sMethodName + " execution Time is : " + lExecutionTime + "sec.");
    }

0

我也有这个需求,我使用了浏览器提供的performance.timeOrigin对象。我在C#中使用它,但是对于这个问题,我修改了@Ripon Al Wasim的答案,使其适用于Java。

import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TimerInSeleniumWebDriver {
    public static void main(String[] args) {
        WebDriver driver;
        driver = new FirefoxDriver();
        //Open your web app (In my case, I opened facebook)
        driver.get("https://www.facebook.com/");
        // Wait for the required any element (I am waiting for Login button in fb)
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l")));
        //Get the time
        long pageLoadTime_ms = getPageLoadTime();
        long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
        System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
        System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
        driver.close();
    }
      public static long getPageLoadTime(WebDriver driver){
        //Creating the JavascriptExecutor interface object by Type casting      
        JavascriptExecutor js = (JavascriptExecutor)driver;     

      //This will get you the time passed since you page navigation started 
      return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");  
    }
}

只需在需要获取页面加载时间的地方调用以下方法。
  public static long getPageLoadTime(WebDriver driver){
        //Creating the JavascriptExecutor interface object by Type casting      
        JavascriptExecutor js = (JavascriptExecutor)driver;     

      //This will get you the time passed since you page navigation started     
      return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");  
    }

你不需要担心页面加载开始的位置,但就像其他答案中所述,你必须在页面加载完成后立即放置它

这类似于StopWatch,但更加简洁(无需启动、停止、重置)。


-12

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

驱动程序.管理().超时().页面加载超时(60, TimeUnit.SECONDS);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接