使用chromedriver导出HAR

时间:2013-08-06 08:52:24

标签: selenium-chromedriver

是否有可能使用chromedriver导出HAR,类似于我用netexpert + firebug用Firefox做的事情?

3 个答案:

答案 0 :(得分:3)

是的,使用BrowsermobProxy您可以使用chromedriver生成HAR文件。

这是python中的脚本,使用Selenium,BrowserMob Proxy和chromedriver以编程方式生成HAR文件。运行此脚本需要用于selenium和browsermob-proxy的Python包。

from browsermobproxy import Server
from selenium import webdriver
import os
import json
import urlparse

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
url = urlparse.urlparse (proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()

答案 1 :(得分:1)

您可以通过chromedriver启用性能日志,并分析网络流量以自行构建HAR。

答案 2 :(得分:1)

请在以下位置检出代码

https://gist.github.com/Ankit3794/01b63199bd7ed4f2539a088463e54615#gistcomment-3126071

步骤:

启用启用日志记录首选项来启动ChromeDriver实例

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("ignore-certificate-errors");
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");

// More Performance Traces like devtools.timeline, enableNetwork and enablePage
Map<String, Object> perfLogPrefs = new HashMap<>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
perfLogPrefs.put("enableNetwork", true);
perfLogPrefs.put("enablePage", true);
chromeOptions.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);

// For Enabling performance Logs for WebPageTest
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
capabilities.setCapability("goog:loggingPrefs", logPrefs);
capabilities.merge(chromeOptions);

从性能日志中获取“消息” JSONObject

private static JSONArray getPerfEntryLogs(WebDriver driver) {
    LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
    JSONArray perfJsonArray = new JSONArray();
    logEntries.forEach(entry -> {
        JSONObject messageJSON = new JSONObject(entry.getMessage()).getJSONObject("message");
        perfJsonArray.put(messageJSON);
    });
    return perfJsonArray;
}

通过传递PerfLogs获得HAR

public static void getHAR(WebDriver driver, String fileName) throws IOException {
    String destinationFile = "/HARs/" + fileName + ".har";
    ((JavascriptExecutor) driver).executeScript(
            "!function(e,o){e.src=\"https://cdn.jsdelivr.net/gh/Ankit3794/chrome_har_js@master/chromePerfLogsHAR.js\",e.onload=function(){jQuery.noConflict(),console.log(\"jQuery injected\")},document.head.appendChild(e)}(document.createElement(\"script\"));");
    File file = new File(destinationFile);
    file.getParentFile().mkdirs();
    FileWriter harFile = new FileWriter(file);
    harFile.write((String) ((JavascriptExecutor) driver).executeScript(
            "return module.getHarFromMessages(arguments[0])", getPerfEntryLogs(driver).toString()));
    harFile.close();
}