import time
import os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait def get_clear_browsing_button(driver):
"""Find the "CLEAR BROWSING BUTTON" on the Chrome settings page."""
return driver.find_element_by_css_selector('* /deep/ #clearBrowsingDataConfirm') def clear_cache(driver, timeout=60):
"""Clear the cookies and cache for the ChromeDriver instance."""
# navigate to the settings page
driver.get('chrome://settings/clearBrowserData')
# wait for the button to appear
wait = WebDriverWait(driver, timeout)
wait.until(get_clear_browsing_button)
# click the button to clear the cache
get_clear_browsing_button(driver).click()
# wait for the button to be gone before returning
wait.until_not(get_clear_browsing_button) """
Use Selenium to Measure Web Timing
Performance Timing Events flow
navigationStart -> redirectStart -> redirectEnd -> fetchStart -> domainLookupStart -> domainLookupEnd
-> connectStart -> connectEnd -> requestStart -> responseStart -> responseEnd
-> domLoading -> domInteractive -> domContentLoaded -> domComplete -> loadEventStart -> loadEventEnd
https://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute
"""
def access_url(driver, url):
# clear_cache(driver)
driver.get(url)
navigation_start = driver.execute_script("return window.performance.timing.navigationStart")
load_end = driver.execute_script("return window.performance.timing.loadEventEnd")
return load_end - navigation_start; def test_url(driver, log_file, url, count):
data = []
for idx in range(count):
load_time = access_url(driver, url)
data.append(load_time)
soted_data = sorted(data)
min_val = soted_data[0]
mid_val = soted_data[count//2]
max_val = soted_data[-1]
avg_val = sum(data)/count log_file.write("test '%s' %d times min:%d mid:%d max:%d avg:%d data:%s\n" %
(url, count, min_val, mid_val, max_val, avg_val, str(data))) if __name__ == "__main__":
pwd_path = os.getcwd()
options = webdriver.chrome.options.Options()
#options.add_argument("--load-extension=" + os.path.join(pwd_path, "Adblock-Plus_v1.13.4"))
options.add_argument("--user-data-dir=" + os.path.join(pwd_path, "chrome_user_data"))
options.add_argument("--start-maximized")
#options.add_extension(os.path.join(pwd_path, "AdBlock_v3.22.1.crx"))
options.add_extension(os.path.join(pwd_path, "Adblock-Plus_v1.13.4.crx"))
driver = webdriver.Chrome(chrome_options=options) url_list = [
#"北京时间",
"https://www.btime.com",
"https://www.btime.com/sports",
"https://item.btime.com/m_2s21ridqq1l",
#"军事头条",
"http://www.tiexue.net/",
"http://bbs.tiexue.net/post2_12837139_1.html",
"http://www.tiexue.net/ShowPicClass_1485_1.html",
]
test_times = 20; with open('chrome_Adblock-Plus.txt', 'w') as log_file:
for url in url_list:
test_url(driver, log_file, url, test_times) driver.close()

python:获取访问访问时的响应时间的更多相关文章

  1. Python类属性访问的魔法方法

    Python类属性访问的魔法方法: 1. __getattr__(self, name)- 定义当用户试图获取一个不存在的属性时的行为 2. __getattribute__(self, name)- ...

  2. 第14.7节 Python模拟浏览器访问实现http报文体压缩传输

    一. 引言 在<第14.6节 Python模拟浏览器访问网页的实现代码>介绍了使用urllib包的request模块访问网页的方法.但上节特别说明http报文头Accept-Encodin ...

  3. Python+Selenium学习--访问连接

    场景 web UI测试里最简单也是最基本的事情就是访问1个链接了. 在python的webdrive中,访问url时应该使用get方法. 代码 #!/usr/bin/env python # -*- ...

  4. Nginx反向代理+Tomcat+Springmvc获取用户访问ip

    Nginx+Tomcat+Springmvc获取用户访问ip 1.Nginx反向代理 修改Nginx配置文件 location / { ***********之前代码*******; proxy_se ...

  5. python学习(22) 访问数据库

    原文链接:http://www.limerence2017.com/2018/01/11/python22/ 本文介绍python如何使用数据库方面的知识. SQLite SQLite是一种嵌入式数据 ...

  6. python基础===成员访问__len__()和__getitem__()

    class A: def __init__(self,*args): self.name = arg pass def __len__(self): return len(self.name) a = ...

  7. 第14.4节 使用IE浏览器获取网站访问的http信息

    上节<第14.3节 使用google浏览器获取网站访问的http信息>中介绍了使用Google浏览器怎么获取网站访问的http相关报文信息,本节介绍IE浏览器中怎么获取相关信息.以上节为基 ...

  8. FireFox每次访问页面时检查最新版本

    FireFox每次访问页面时检查最新版本 浏览器都有自己的缓存机制,作为开发人员,每次js的修改都要清空缓存,显然很不方便.而firefox并没有提供ie那样的设置. 下面的方法就可以非常方便的设置f ...

  9. Linux分析日志获取最多访问的前10个IP

    原文地址:http://xuqq999.blog.51cto.com/3357083/774714 apache日志分析可以获得很多有用的信息,现在来试试最基本的,获取最多访问的前10个IP地址及访问 ...

  10. 访问网页时提示的503错误信息在IIS中怎么设置

    访问网页时提示的503错误信息在IIS中怎么设置 503是一种常见的HTTP状态码,出现此提示信息的原因是由于临时的服务器维护或者过载,服务器当前无法处理请求则导致了访问网页时出现了503错误.那么当 ...

随机推荐

  1. mycat配置安装测试

    https://www.jianshu.com/p/26513f428ecf #下载安装#java jdk mkdir /usr/local/java/tar -zxvf jdk-7u80-linux ...

  2. 解决 VUE 微信登录验证 【感谢原文:https://segmentfault.com/a/1190000009493199】

    [感谢原文:https://segmentfault.com/a/1190000009493199] SPA单页应用中微信授权登录的一点思路 单页应用应该如何解决微信授权登录的尴尬跳转?后退无法返回? ...

  3. 《JavaScript设计模式与开发》笔记 2.this指针

    1.作为对象方法调用 2.作为普通函数调用 1.作为普通函数进行调用 2.将函数赋值给一个对象 3.callback内部的this指向了window 4.解决callback内部的this问题 3.构 ...

  4. AM二次开发中选择指定范围内的对象

    使用Spatial可以快速选择指定范围内的对象 例如下面的代码可以选择所有在[0,0,0]-[10m,10m,10m]这个盒子之内的对象: 其中ElementsInBox还可以指定对象类型做进一步筛选 ...

  5. 自动控制中常用的Matlab命令

    部分分式展开:[r p k]=residue[num,den] 传递函数到状态空间:[A, B, C, D]=tf2ss(num, den)  % 状态空间不唯一,Matlab给出其中一种 状态空间到 ...

  6. @Resource无法import相关

    一.查找@Resource在那个jar包里 @Resource注解的类路径是:javax.annotation.Resource,是annotation-api.jar包中的类,是JDK1.6支持的注 ...

  7. Nexus3.6版私服搭建安装与配置教程

    1.本地环境配置(Nexus3.6支持jdk版本1.6.1.7.1.8) 1.1.官网下载地址:https://www.sonatype.com/download-oss-sonatype       ...

  8. 【Graphite学习】系列学习文章-【转】

    Graphite 系列 #2:Carbon 和 Whisper GRAPHITE SERIES #1: PROVISION HARDWARE GRAPHITE SERIES #2: CARBON &a ...

  9. jdbi

    JDBI是一个使用方便的SQL开发库,用符合Java语言习惯的集合.Bean等方式,提供关系数据库访问接口,同时保留了JDBC类似的信息.JDBI提供了链式和SQL两种风格的API. jdbi的网址是 ...

  10. Android定位服务关闭和定位(悬浮)等权限拒绝的判断

    public void checkLocationPermission() { if (!PermissionHelper.isLocServiceEnable(this)) {//检测是否开启定位服 ...