In this blog, we will learn about what is a proxy server in selenium and how to set a proxy in selenium 4, and how we connect the website using a proxy in selenium.
What is Proxy
Proxy is an intermediate server that sits between you and the actual server from where your response is coming in an easy way you can think of it as a client and server concept where you are a client or your system is a client and the server is an actual server where the response is coming and between the client and the server sits an intermediate server which is known as a proxy server. A proxy can also provide an added layer of security by operating as a firewall between client and web servers.
Why do we need the Proxy Server?
Proxy server act as an intermediary for requests between a client and a server. In a very simple way whenever you have to intercept the network traffic you have to access some site under corporate restrictions and mock some requests you can use a proxy
- It can capture network traffic
- Mock backend calls made by the website it can use for the testing
- Access a website under corporate restrictions/policies
How to Set a proxy server
Some of the proxy servers have not required a username and password which means some server has not required any type of authentication following are the steps of how to set up the proxy server.
- Import Selenium WebDriver from the package
- Define the proxy server IP and port that you want to use
- Set ChromeOptions() and set the desire capabilities
- Add the options to the Chrome
How do we use Proxy in Selenium
You can do an additional step if you have some kind of monitor to capture your traffic or capture the proxy requests you can use some tool called Charles proxy which is used for monitoring and debugging the proxy. This is an HTTP monitor and you can check all your traffic. You can simply download the Charles in the system and set it up in the same way in any software.
Let us see a Demo
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.Proxy;
public class proxy {
public static void main(String[] args){
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
//proxy.setHttpproxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://google.com/");
driver.quit();
}
}
- Let’s create a proxy object we can set HTTP proxy, SSL proxy, and all of the things and we will give the IP and the port that you want to use for the proxy server so for proxy you will be have the IP and a port that you can also get from the network team or whatever application you are using you must be knowing it here we are using localhost and port is 8080.
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
// proxy.setHttpproxy("localhost:8080")
proxy.setSslProxy("localhost:8080");
- Then after this, we will use an options class which is the replacement of over desire capabilities. if you are using selenium 4 you should not be using desired capabilities that will not work. we will use the options class and for every browser, we have the options. here we are using chrome and we have chrome options after that set the capabilities for proxy and pass the proxy object. now we will start the chrome browser and we will be giving the chrome options in the chrome driver now we will go to any website like https://google.com.and now we will run the script.
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
References
https://www.selenium.dev/documentation/webdriver/