【问题标题】:Selenium Python Load Page and Script (Firefox and IE)Selenium Python 加载页面和脚本(Firefox 和 IE)
【发布时间】:2016-09-10 06:33:18
【问题描述】:

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

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

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

PS 我用的是 IE 或 Firefox

【问题讨论】:

  • 你提前知道javascript底层框架是什么吗?
  • 我可以知道,但我正在尝试为每个页面编写一个通用脚本,我希望 selenium 等待加载所有页面,然后再开始执行其他操作

标签: python performance selenium python-webbrowser


【解决方案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)

【讨论】:

  • 非常感谢;) 相反,我如何检查何时完成加载所有 HTML?有没有类似的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 2019-04-05
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多