Can we get the HTTP Response Code in Selenium with Java?


I have these Selenium WebDriver tests and I want to check if the HTTP Request returns any HTTP 403 Forbidden. Can I get these HTTP response status code with Selenium WebDriver? Maybe display it in the logger or save to a file?



The following is the code where we can get the HTTP response code in Selenium with Java.

import java.util.Iterator;

import java.util.logging.Level;

import org.json.JSONException;

import org.json.JSONObject;



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.DesiredCapabilities;

public class TestResponseCode {

    public static void main(String[] args) {

        // simple page (without many resources so that the output is

        // easy to understand

        String url = "";



    private static void downloadPage(String url) {

        System.setProperty("", "D:\\Softwares\\Selenium\\Drivers\\ChromeDrivers\\75.0.3770.140\\chromedriver.exe");

        ChromeDriver driver = null;

        try {

            ChromeOptions options = new ChromeOptions();

            // add whatever extensions you need

            // for example I needed one of adding proxy, and one for blocking

            // images

            // options.addExtensions(new File(file, ""));

            // options.addExtensions(new File("extensions",

            // "Block-image_v1.1.crx"));

            DesiredCapabilities cap =;

            cap.setCapability(ChromeOptions.CAPABILITY, options);

            // set performance logger

            // this sends Network.enable to chromedriver


             * LoggingPreferences logPrefs = new LoggingPreferences();

             * logPrefs.enable(LogType.PERFORMANCE, Level.ALL);

             * cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);


            LoggingPreferences logPrefs = new LoggingPreferences();

            logPrefs.enable(LogType.PERFORMANCE, Level.ALL);

            options.setCapability("goog:loggingPrefs", logPrefs);

            driver = new ChromeDriver(options);

            // navigate to the page

            System.out.println("Navigate to " + url);


            // and capture the last recorded url (it may be a redirect, or the

            // original url)

            String currentURL = driver.getCurrentUrl();

            LogEntries logs = driver.manage().logs().get("performance");

            int status = -1;

            System.out.println("\\nList of log entries:\\n");

            for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {

                LogEntry entry =;

                try {

                    JSONObject json = new JSONObject(entry.getMessage());


                    JSONObject message = json.getJSONObject("message");

                    String method = message.getString("method");

                    if (method != null && "Network.responseReceived".equals(method)) {

                        JSONObject params = message.getJSONObject("params");

                        JSONObject response = params.getJSONObject("response");

                        String messageUrl = response.getString("url");

                        if (currentURL.equals(messageUrl)) {

                            status = response.getInt("status");

                            System.out.println("---------- bingo !!!!!!!!!!!!!! returned response for " + messageUrl

                                    + ": " + status);

                            System.out.println("---------- bingo !!!!!!!!!!!!!! headers: " + response.get("headers"));



                } catch (JSONException e) {

                    // TODO Auto-generated catch block




            System.out.println("\nstatus code: " + status);

        } finally {

            if (driver != null) {






If you want to unleash your potential in this competitive field, please visit the selenium course page for more information, where you can find the selenium tutorials and selenium frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
Protected by Astra Security