Skip to content

Is it possible to extract any Header value from the Network panel on google chrome developer tools with selenium?

I want to access the value of one of the Headers under the Network panel on google chrome developer tools with selenium.

I’m able to navigate and access the json for Networks panel using ChromeOptions in Selenium. But i also need to extract a value of any of the Headers for any Name. For ex. I manually navigated to www.google.com, press F12, click on any name from the left column and for that particular name, in the right hand side, i need to access the “Request URL” under Headers section.

Using the below code, i’m able to access the Networks panel:

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/root/Downloads/aaa"); 
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);[enter image description here][1]
driver.get("http://www.google.com");
String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();

Is it possible through Selenium or also through any Java program?

Answer

you need to use LogType.performance to get that. It will contain a lot of info and you can filter accordingly.

public class travelComps {
    static ChromeDriver driver;

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver",
                "Chromedriver76PAth");

        ChromeOptions cop = new ChromeOptions();

        LoggingPreferences loggingprefs = new LoggingPreferences();
        loggingprefs.enable(LogType.BROWSER, Level.ALL);
        loggingprefs.enable(LogType.CLIENT, Level.ALL);
        loggingprefs.enable(LogType.PERFORMANCE, Level.ALL);
        loggingprefs.enable(LogType.PROFILER, Level.ALL);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
        cop.setCapability("goog:loggingPrefs", loggingprefs);

        cop.merge(capabilities);
        driver = new ChromeDriver(cop);
        driver.get("http://automationpractice.com/index.php");

        logBrowserConsoleLogs();

        driver.quit();

    }

    private static void logBrowserConsoleLogs() {

        all(LogType.PERFORMANCE);

    }

    public static void all(String logTypes) {
        System.out.println("================== " + logTypes + "  LOGS =======================");

        List<LogEntry> logEntries = driver.manage().logs().get(logTypes).getAll();
        for (LogEntry entry : logEntries) {
            System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
        }
        System.out.println("=======================================================");
    }