- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we get the HTTP Response Code in Selenium with Java?
We can get the HTTP response code in Selenium webdriver with Java. Some of the response codes are – 2xx, 3xx, 4xx and 5xx. The 2xx response code signifies the proper condition, 3xx represents redirection, 4xx shows resources cannot be identified and 5xx signifies server problems.
To obtain the response code we shall use the HttpURLConnection class. To have a link to the URL, the method openConnection is used. Also, we have to use the setRequestMethod where the vale Head is to be passed as a parameter.
We have to create an instance of the HttpURLConnection class and then apply the connect method on it. Finally, to obtain the response code the method getResponseCode is used.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class GetHttpResponse{ public static void main(String[] args) throws MalformedURLException, IOException { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); // establish, open connection with URL HttpURLConnection cn = (HttpURLConnection)new URL("https://www.tutorialspoint.com/index.htm ").openConnection(); // set HEADER request cn.setRequestMethod("HEAD"); // connection initiate cn.connect(); //get response code int res = cn.getResponseCode(); System.out.println("Http response code: " + res); driver.quit(); } }
Output
- Related Articles
- How to get HTTP Response Code using Selenium WebDriver?
- How to get Response Status Code with Selenium WebDriver?
- The best way to inspect HTTP response headers with Selenium
- Checking HTTP Status Code in Selenium.
- How to get the Response status code in Golang?
- Explain the HTTP request-response lifecycle in ASP.NET Core.
- How to get Selenium to wait for ajax response?
- In MySQL, how can we get the number code of a particular character?
- Can we write any code after throw statement in Java?
- Can we get the supported image types in Java
- How can we handle authentication popup in Selenium WebDriver using Java?
- How can we restrict access to methods with specific HTTP verbs in C# ASP.NET\nWebAPI?
- Sending HTTP error code using Express.js
- How to get HTML code of a WebElement in Selenium?
- Get page title with Selenium WebDriver using Java.

Advertisements