How to get HTTP Response Code using Selenium WebDriver

0 votes
I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden.

Is it possible to get the HTTP response status code with Selenium WebDriver?
Nov 27, 2020 in Selenium by anonymous
• 8,880 points
110 views

1 answer to this question.

0 votes

You can use BrowserMob proxy to capture the requests and responses with a HttpRequestInterceptor. Here is an example in Java:

// Start the BrowserMob proxy
ProxyServer server = new ProxyServer(9978);
server.start();

server.addResponseInterceptor(new HttpResponseInterceptor()
{
    @Override
    public void process(HttpResponse response, HttpContext context)
        throws HttpException, IOException
    {
        System.out.println(response.getStatusLine());
    }
});

// Get selenium proxy
Proxy proxy = server.seleniumProxy();

// Configure desired capability for using proxy server with WebDriver
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// Set up driver
WebDriver driver = new FirefoxDriver(capabilities);

driver.get("http://stackoverflow.com/questions/6509628/webdriver-get-http-response-code");

// Close the browser
driver.quit();
answered Nov 27, 2020 by Gitika
• 65,850 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

How to get status code by using selenium.py (python code)

Unfortunately, Selenium does not provide this information ...READ MORE

answered Aug 16, 2018 in Selenium by Meci Matt
• 9,460 points
3,222 views
0 votes
2 answers

How to get a page name using Selenium Webdriver (C#)?

String pageTitlte=driver.getTitle(); READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,010 points
838 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
4,323 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,246 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,127 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
3,625 views
+1 vote
4 answers

How to get typed text from a textbox by using Selenium Webdriver?

Hey Ashmita, to get the typed text ...READ MORE

answered Jun 24, 2019 in Selenium by Abha
• 28,000 points
14,923 views
0 votes
6 answers

How to specify "ENTER" button functionality in Selenium WebDriver code?

using OpenQA.Selenium.Interactions; Actions builder = new Actions(driver); ...READ MORE

answered Feb 12, 2019 in Selenium by anonymous
51,010 views