How To Get The HTTP Status Code In Selenium WebDriver

By , 16 November 2012

How To Get The HTTP Status Code In Selenium WebDriver
How To Get The HTTP Status Code In Selenium WebDriver

One of the first things you'll notice when you start testing your web application with Selenium WebDriver is that there is no API to get the HTTP status code for a page. If you want to know why, you can go and read WebDriver issue #141. Personally I don't care why - I just want to test my HTTP reponse codes (especially 403 Permission Denied).

There is a fairly simple workaround you can use for WebDriver, but firstly lets look at how to do it without WebDriver. Nobody said you HAVE to use Selenium right?

How To Get The HTTP Status Code In Selenium WebDriver

Apache HTTPClient

This is a Java library use for general HTTP network communication and includes supports sessions via cookies. Their fluent API makes it relatively simple to get the response code for a URL.

    public int getResponseCode(String url) {
        try {
            return Request.Get(url).execute().returnResponse().getStatusLine()
        } catch (Exception e) {
            throw new RuntimeException(e);

You can easily add the HTTPClient fluent api to your Java project via maven:


The disadvantage of HTTPClient is it is doesn't include a browser like Selenium, although you can create sessions which might be useful for testing more complex use cases.


HTMLUnit is a headless Java browser and is actually one of the browsers supported by WebDriver. It's very easy to get the response code with HTMLUnit:

    public int getResponseCode(String url) {
        try {
            WebClient client = new WebClient();
            return client.getPage(url).getWebResponse().getStatusCode();
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);

This library is also available from Maven.


Selenium WebDriver

Finally we get to WebDriver. If you've written a few tests with WebDriver already you're probably used to this API, only there is no native support for inspecting response headers. The simple workaround is to write the response code to your response output.

For example, on my 403 Permission Denied page, I have:

   <h1 id="web_403">403 Access Denied</h1>

which can be easily checked via the WebDriver API:

    public boolean is403(WebDriver driver) {
        try {
            return true;
        } catch (NoSuchElementException e) {
            return false;

You can generalise the idea for most HTTP responses that don't cause a redirect by adding a meta field to your <head> section. In JSF / Servlets 3.0 you do it like this:

    <meta id="web_response" name="response" 


This can be tested easily with WebDriver:

     * Selenium doesn't give you access to the response headers, so we parse 
     * the content. This relies on the web_response meta tag in the head 
     * section of the output.
    public int getResponseCode(WebDriver driver) {
        return Integer.parseInt(driver.findElement("web_response"))

Note for Servlets before version 3.0 you will need to implement your own response.getStatus() method and I'm told that in PHP you use $_SERVER['REDIRECT_STATUS'] for versions < 5.4 and http_response_code()for newer versions. Let me know how it is done in your language in the comments below.

This solution works pretty well and is a lot more productive than bashing on the Selenium developers for the gaping hole in their API.

So get testing slacker!



About I Like Spam

How To Get The HTTP Status Code In Selenium WebDriver

Leave a Comment

Please visit to add your comments.

Comment posted by: roy, 2 years ago
Comment posted by: Ajay, 2 years ago

The above code only works well with success code ie. 200.for all other codes exception is thrown without returning the response code.

I have tried with Apache HTTPClient and for web driver code getting error

Comment posted by: kav123, 3 years ago

I badly need the HTTP header request that I make for a url setting proxy and user agent.

How do you get it done in java? I see the above code for apache HTTP client, which I can't do it in Firefox web driver. My application is built on firefox completely. I need to test this to see whether my HTTP request is correct or not?

Have you worked around to get it done in firefox?


Comment posted by: Adam Goucher, 4 years ago

Well, clearly you would know better than I. In the meanwhile, I'll be busy succeeding with it.

Comment posted by: , 4 years ago

When compared to the fact that we're using HTTP and HTML to build complex applications, this doesn't seem like such an egregious hack.

Comment posted by: Yaci, 4 years ago

 @Adam: BrowserMob is

  1. Not very reliable
  2. Still in beta phase
  3. It's an abandoned project (AFAIK the main developer Patrick Lightbody left Neustar and the future of the project is unknown)

It's better to use Firebug+Netexport, but this works only on Firefox. Sure you're right - using a proxy should be the correct way to go, but as long as there are no reliable solutions we have to rely on hacks like the one above.

Comment posted by: Adam Goucher, 4 years ago

Or. You could route your scripts through a proxy that has an API that exposes this to you. Like say, the BrowserMob Proxy, which has a nice integration (as far as these things go) with WebDriver -- and will work with any browser, and any language.

But hey, if you want to do more work than necessary whilst trolling, I suppose that is up to you.