Search code examples
javaseleniumselenium-webdrivernetwork-programmingselenium-chromedriver

Capture network traffic using selenium webdriver java 4.0v


I would like to capture the network traffic generated in a Chromedriver window. I have found out that it can be done using selenium 4.0 DevTools utility but I can´t find how to or a good documentation.

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/devtools/DevTools.html

Is there an easiest way to do? Thanks

enter image description here


Solution

  • You can get this done using LoggingPreferences and ChromeOptions

    imports

    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.logging.LogEntries;
    import org.openqa.selenium.logging.LogEntry;
    import org.openqa.selenium.logging.LogType;
    import org.openqa.selenium.logging.LoggingPreferences;
    import org.openqa.selenium.remote.CapabilityType;
    

    Here We get Json String which contain data about the in log records. I use json-simple library to convert the received json String to JSONObject.

    LoggingPreferences preferences = new LoggingPreferences();
    preferences.enable(LogType.PERFORMANCE, Level.ALL);
    
    ChromeOptions option = new ChromeOptions();
    option.setCapability(CapabilityType.LOGGING_PREFS, preferences);
    option.setCapability("goog:loggingPrefs", preferences);
    option.addArguments();
    
    System.setProperty("webdriver.chrome.driver", "chrome_driver_path");
    
    ChromeDriver chromeDriver = new ChromeDriver(option);
    chromeDriver.manage().window().maximize();
    this.driver = chromeDriver;
    
    
    driver.get("website_url");
    
    LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
    for (LogEntry entry : logs) {
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = null;
        try {
            jsonObject = (JSONObject) parser.parse(entry.getMessage());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        JSONObject messageObject = (JSONObject) jsonObject.get("message");
        System.out.println(messageObject.toJSONString());
        // You can do the required processing to messageObject
    }
    

    You can filter the type of network calls from logs using type (XHR, Script, Stylesheet) in the json String.

    for (LogEntry entry : logs) {
       if(entry.toString().contains("\"type\":\"XHR\"")) {
       }
    }