使用 Selenium WebDriver 瞭解載入頁面的具體時間?


我們可以使用 Selenium webdriver 確定載入頁面的具體時間。藉助 System.currentTimeMillis 方法,可以捕獲頁面載入之前的時間。

在應用程式的 URL 啟動後,我們必須藉助顯式等待條件等待頁面完全載入。一旦達到元素的預期條件,我們將再次記錄當前時間。

在頁面載入之前和之後記錄的時間差將測量出載入頁面的具體時間。

語法

long s = System.currentTimeMillis();

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class PageLoadTime{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //capture time before page load
      long s = System.currentTimeMillis();
      //URL launch
      driver.get("https://tutorialspoint.tw/videotutorials/subscription.php");
      //verify page is loaded
      WebDriverWait wt = new WebDriverWait(driver,6);
      wt.until(ExpectedConditions.elementToBeClickable (By.className("s-buy")));
      //capture time after page load
      long e = System.currentTimeMillis();
      //compute time
      long r = e – s;
      System.out.println("Page load time in milliseconds: " + r);
      driver.close();
   }
}

輸出

更新於: 06-Apr-2021

4 千 + 次瀏覽

開啟您的 職業

完成課程獲得認證

開始
廣告