有什么解决方案可以获得加载时间吗


from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('https://pythonbasics.org')
timeout = 3
try:
element_present = EC.presence_of_element_located((By.ID, 'main'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print("Timed out waiting for page to load")
finally:
print("Page loaded")

这是我的代码,我想得到等待时间,例如,如果加载某个xpath类需要1分钟,那么得到这个时间

你可以这样做:

import time
driver = webdriver.Firefox()
driver.get('https://pythonbasics.org')
timeout = 100
try:
start_time = time.time()
element_present = EC.presence_of_element_located((By.ID, 'main'))
WebDriverWait(driver, timeout).until(element_present)
diff_time = time.time() - start_time
print("It takes {} seconds to find element".format(diff_time))
except TimeoutException:
print("Timed out waiting for page to load")
finally:
print("Page loaded")

@jaSON给出的答案是正确的。但还有另一种选择,那就是python库timeit中的timeit方法。你可以通过多种方式使用它。我会给你正确解释文章的超链接
[Python Timit((]:https://www.guru99.com/timeit-python-examples.html#:~:text=Python%20timeit((%20is%20a,给定%20set%20of%20code%20snippets。

相关内容

  • 没有找到相关文章

最新更新