Selenium Python Load Page and Script (Firefox and IE)

家住魔仙堡 提交于 2019-12-02 17:45:58

问题


I don't really have idea about that so I'd like you to give me some advice if you can.

Generally when I use Selenium I try to search the element that I'm interested in, but now I was thinking to develop some kind of performance test so check how much time take a specific webpage (html, script, etc...) to load.

Do you have some idea how to know the load time of html, script etc without search for a specific element of the page?

PS I use IE or Firefox


回答1:


You could check the underlying javascript framework for active connections. When there are no active connections you could then assume the page is finished loading.

That, however, requires that you either know what framework the page uses, or that you must systematically check for different frameworks and then check for connections.

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)


来源:https://stackoverflow.com/questions/37220547/selenium-python-load-page-and-script-firefox-and-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!