0

我真的不知道,所以如果可以的话,我希望你给我一些建议。

通常,当我使用 Selenium 时,我会尝试搜索我感兴趣的元素,但现在我正在考虑开发某种性能测试,以便检查特定网页(html、脚本等)需要多少时间加载。

您是否知道如何在不搜索页面特定元素的情况下知道 html、脚本等的加载时间?

PS 我使用 IE 或 Firefox

4

1 回答 1

1

您可以检查底层 javascript 框架以获取活动连接。当没有活动连接时,您可以假设页面已完成加载。

但是,这要求您要么知道页面使用什么框架,要么必须系统地检查不同的框架,然后检查连接。

def get_js_framework(driver):
    frameworks = [
        'return jQuery.active',
        'return Ajax.activeRequestCount',
        'return dojo.io.XMLHTTPTransport.inFlight.length'
    ]

    for f in frameworks:
        try:
            driver.execute_script(f)
        except Exception:
            logging.debug("{0} didn't work, trying next js framework".format(f))
            continue
        else:
            return f
    else:
        return None


def load_page(driver, link):
    timeout = 5
    begin = time.time()

    driver.get(link)
    js = _get_js_framework(driver)

    if js:
        while driver.execute_script(js) and time.time() < begin + timeout:
            time.sleep(0.25)
    else:
        time.sleep(timeout)
于 2016-05-16T13:57:16.810 回答