问题描述
我正在使用 chrome 选项来访问使用 selenium 的性能日志记录,我正在尝试编写一个代码来帮助我计算出 http 请求的总数和加载完成后页面的大小.我们可以使用开发工具的网络选项卡手动检查这一点.只需要知道如何访问网络表的值或汇总值.因为性能日志没有给我我需要的汇总值,我想写一个代码来获取:
I'm using chrome option to access the performance logging using selenium, I'm trying to write a code that would help me figure out the total number of the http request and the size of the page after the loading is finished. Manually we can check this by using Dev tool's network tab. Just need to know how to access the Network table's value or summary values. Because the performance logging is not giving me the summarized values that I need, I would like to write a code to get:
请求总数=
页面的总重量是多少=
如果可能的话.
网络选项卡的屏幕截图突出显示了我需要访问的摘要和请求表值一个>
capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = {'browser': 'DEBUG'}
capabilities['loggingPrefs'] = {'performance': 'ALL'}
capabilities['perfLoggingPrefs'] = {'enableTimeline': 'true'}
driverLocation = "/Users/harisrizwan/Selenium/chrome/chromedriver"
os.environ["chrome.driver"] = driverLocation
chrome_options = Options()
chrome_options.add_argument("headless")
driver=
webdriver.Chrome(driverLocation,desired_capabilities=capabilities)
driver.implicitly_wait(10)
driver.maximize_window()
baseUrl="www.google.com"
driver.get(baseUrl)
使用 pandas 创建日志的数据框.
df = pd.DataFrame(driver.get_log('performance'))
df.to_clipboard(index=False)
谢谢.
推荐答案
我不确定计算页面的总权重,但可以获取发送到服务器的请求总数
I"m not sure on calculating total weight of the page but obtaining total number of request sent to the server is possible
使用 https://github.com/lightbody/browsermob-proxy 并添加代理到desired_capabilities,一旦脚本完成,将har文件转储到json并获取所有请求
Use https://github.com/lightbody/browsermob-proxy and add the proxy to the desired_capabilities, once the script finished, dump the har file to json and get all the request
在蟒蛇中:
from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import json
server = Server('path to the proxy server file')
server.start()
proxy = server.create_proxy()
options = Options()
options.add_argument(f'--proxy-server={proxy.proxy}')
driver = webdriver.Chrome('path to chromedriver', desired_capabilities=options.to_capabilities())
proxy.new_har()
driver.get('https://www.google.com')
result = json.dumps(proxy.har)
json_data = json.loads(result)
request= [x for x in json_data['log']['entries']]
server.stop()
driver.close()
这篇关于如何在 python/java 中使用 Selenium 访问 Chrome 的开发工具网络选项卡的请求或摘要的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!