我们如何获得使用 Selenium WebDriver 加载页面的准确时间?

我们如何获得使用 Selenium WebDriver 加载页面的准确时间?

How can we get exact time to load a page using Selenium WebDriver?

我们使用 Thread.sleep

We use Thread.sleep

我们使用隐式等待

我们使用 WebDriverWait

we use WebDriverWait

但是我们如何才能获得使用 Selenium WebDriver 加载页面的准确时间呢?

but How can we get exact time to load a page using Selenium WebDriver?

推荐答案

如果您想了解使用 Selenium WebDriver(又名 Selenium 2)完全加载页面需要多长时间.

If you are trying to find out how much time does it take to load a page completely using Selenium WebDriver (a.k.a Selenium 2).

通常,WebDriver 只有在页面完全加载后才会将控制权返回给您的代码.

Normally WebDriver should return control to your code only after the page has loaded completely.

因此,以下 Selenium Java 代码可能会帮助您找到页面加载时间 -

So the following Selenium Java code might help you to find the time for a page load -

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); 

如果这不起作用,那么您将不得不等到页面上显示某些元素 -

If this does not work then you will have to wait till some element is displayed on the page -

 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); 

相关文章