Hello, Welcome to Selenium tutorials in this post we will see How to Handle Proxy in Selenium Webdriver.When you try to access some secure applications you will get proxy issues so many times. Until we do not set proxy, we cannot access the application itself.
Some application also has SSL certificate enabled or you can say as the secure site like banking, insurance company etc. To access these site you also have to handle certificates it is expired. I have one article which will discuss how to handle untrusted certificate in Selenium Webdriver.
How to Handle Proxy in Selenium Webdriver
You have now two option to handle this so based on your preferences you can use any one of this. I always consider the second approach.
1- Change the proxy setting manually and open default browser
2- Change the proxy setting using Webdriver code.
In this post, we will see the second approach
To handle proxy setting in Selenium we have a separate class called Proxy that is available inside org.openqa.selenium package
Approach-
1-Create object of proxy class and set HTTP proxy or FTP proxy based on requirement
These are methods available for the proxy.
2 -Use DesiredCapability class to customize capability of browser and pass the proxy object.
3- While initiating browser pass capability object
Let’s implement the same-How to Handle Proxy in Selenium Webdriver
package blog;
import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class ProxyDemo { public static void main(String[] args) { // Create proxy class object Proxy p=new Proxy(); // Set HTTP Port to 7777 p.setHttpProxy("localhost:7777"); // Create desired Capability object DesiredCapabilities cap=new DesiredCapabilities(); // Pass proxy object p cap.setCapability(CapabilityType.PROXY, p); // Open firefox browser WebDriver driver=new FirefoxDriver(cap); // from here onwards code will be same as normal script } }
Once you execute above program, proxy setting will be set in browser, you can check through browser setting
You can see the proxy settings has been added now.
Point- Let’s assume you have set of 100 test cases and now you have to make changes in all test right ? What you will do.
Ans- I always use the Base class concept of Selenium Webdriver which avoid this over work for me. I will also suggest you to apply the same in your code.
Thanks for visiting my blog. Please comment below if you finding any issue.
Keep in touch. Have a nice day 🙂
manoj says
I am using this code but not working!!
Mukesh Otwani says
Hi manoj do not use autoDetect method. You can set your proxy directly.
S. Shaikh says
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(“network.proxy.type”, 1);
profile.setPreference(“proxy.abc.com”, 6050);
WebDriver driver = new FirefoxDriver(profile);
driver.get(“http://www.google.com”);
i am running this code but still not able to open the google page…pls pls help
Mukesh Otwani says
Hey Shaikh,
Google works without proxy so once you set proxy it wont work.
Rajan singh says
How to set proxy in chrome
Mukesh Otwani says
Hey Ranjan,
You can use DesiredCapability class to set proxy for Chrome and other browsers.