Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用selenium记录mathjax加载时间_Python_Selenium_Mathjax_Execute Script - Fatal编程技术网

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

Python 如何使用selenium记录mathjax加载时间,python,selenium,mathjax,execute-script,Python,Selenium,Mathjax,Execute Script,我尝试使用selenium获取异步元素(MathJax等式)加载时间 我试图编写一个python selenium脚本来记录我的网站的加载时间,但是我的网站包含了很多由Mathjax异步转换的公式,因此我无法正确记录它 我尝试先用“performance.timening”来记录加载时间,但它只能为我提供“加载时间” 从selenium导入webdriver source=“url” driver=webdriver.Chrome() 获取驱动程序(源代码) navigationStart=dr

我尝试使用selenium获取异步元素(MathJax等式)加载时间

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

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

从selenium导入webdriver
source=“url”
driver=webdriver.Chrome()
获取驱动程序(源代码)
navigationStart=driver.execute_脚本(“返回窗口.性能.计时.navigationStart”)
loadEventEnd=driver.execute\u脚本(“返回窗口.performance.timening.loadEventEnd”)
加载时间=加载事件结束-导航开始
然后,我尝试定位“MathJax”的ID,并等待加载一个MathJax元素(例如“MathJax-element-1-Frame”)

从selenium导入webdriver
导入时间
从selenium.webdriver.support.wait导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
从selenium.webdriver.common.by导入
source=“url”
driver=webdriver.Chrome()
begin=time.time()
获取驱动程序(源代码)
定位器=(By.ID,'MathJax-Element-1-Frame')
WebDriverWait(驱动程序,20,0.5)。直到(EC.存在元素(定位器))
end=time.time()
完成时间=结束-开始
但时间并非绝对正确

尝试使用
timedelta
即:

以微秒分辨率表示两个datetime实例之间差异的持续时间


更新 这是一个等待所有
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")
def等待直到加载(驱动程序,秒数:int=30)->无:
java_script_to_load=“var injector=window.angular.element('body').injector();”\
“var$http=injector.get('$http');”\
“返回($http.pendingRequests.length==0);”
end_time=datetime.utcnow()+timedelta(秒=秒)
打印(“等待所有元素…”)

虽然datetime.utcnow()Thx很多,但我的问题不是精确性。我的问题是因为MathJax加载是异步的,记录时间不正确,因为我不知道MathJax何时完成加载。对不起,我的描述不好:(@OliverL我添加了一个等待元素加载的函数,请尝试!
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")