python - 如何使用 selenium 记录 mathjax 加载时间

标签 python selenium mathjax execute-script

我正在尝试使用 selenium 获取异步元素(MathJax 方程)加载时间。

我尝试编写一个python-selenium脚本来记录我的网站的加载时间,但是我的网站包含很多由Mathjax异步转换的方程,因此我无法正确记录它。

我尝试先使用“performance.timing”来记录加载时间,但它只能为我提供“加载时间”。

from selenium import webdriver
source = "url"
driver = webdriver.Chrome()
driver.get(source)
navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
loadEventEnd = driver.execute_script("return window.performance.timing.loadEventEnd")
load_time = loadEventEnd - navigationStart

然后,我尝试找到“MathJax”的 ID,并等待加载一个 mathjax 元素(例如“MathJax-Element-1-Frame”)

from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
source = "url"
driver = webdriver.Chrome()
begin = time.time()
driver.get(source)
locator = (By.ID, 'MathJax-Element-1-Frame')
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
end = time.time()
finish_time = end - begin

但是时间并不绝对正确。

最佳答案

尝试使用datetime.utcnow() timedelta 是:

A duration expressing the difference between two datetime instances to microsecond resolution.

from datetime import datetime, timedelta
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
source = "url"
driver = webdriver.Chrome()
begin = datetime.utcnow() + timedelta(1)
driver.get(source)
locator = (By.ID, 'MathJax-Element-1-Frame')
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
end = datetime.utcnow() + timedelta(1)
finish_time = end - begin
<小时/>

更新

这是一个等待所有pendingRequests加载的函数。 它可能对您的情况也有帮助。

def wait_until_loaded(driver, seconds: int = 30) -> None:
    java_script_to_load = "var injector = window.angular.element('body').injector();"\
                                  " var $http = injector.get('$http');" \
                                  "return ($http.pendingRequests.length === 0);"
    end_time = datetime.utcnow() + timedelta(seconds=seconds)
    print("wait for All Elements....")
    while datetime.utcnow() <= end_time:
        try:
            if driver.execute_script(java_script_to_load):
                print(f"loaded in"
                      f" {datetime.utcnow() + timedelta(seconds=seconds) - end_time}seconds")
                sleep(1)
                return
        except WebDriverException:
            continue
        sleep(0.1)
    raise TimeoutError("waiting for elements for too long")

希望这对您有帮助!

关于python - 如何使用 selenium 记录 mathjax 加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56627965/

相关文章:

javascript - MathJax 在 Jquery .click 事件中不起作用

python - 从由空格分隔的单个输入整数列表创建二维数组

javascript - 如何向 PHPMyAdmin 添加 Mathjax 支持

python - 来自 EC2 通过 Gmail 的 Django send_mail() 给出 SMTPAuthenticationError - 但在 localhost 中工作正常

python - 如何在失败的情况下调用 py.test 中的 'debug' 方法?

java - 即使输入新值后,文本框的值属性仍保留旧值

java - 如何从 selenium webdriver 的子菜单中选择下拉菜单

github - 如何使用 org-mode 在 github 上创建个人 wiki+博客?

python - 为什么 signal.SIGALRM 在 Windows 上的 Python 中不起作用?

python - 一起使用 Tkinter 和 pygame 有什么我需要知道的吗?