Advance Selenium

How to Handle Proxy in Selenium Webdriver

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.

 

How to Handle Proxy in Selenium Webdriver
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

How to Handle Proxy in Selenium Webdriver

 

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 🙂

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

6 thoughts on “How to Handle Proxy in Selenium Webdriver

  1. manoj says:

    I am using this code but not working!!

    1. Hi manoj do not use autoDetect method. You can set your proxy directly.

  2. 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

    1. Hey Shaikh,

      Google works without proxy so once you set proxy it wont work.

      1. Rajan singh says:

        How to set proxy in chrome

        1. Hey Ranjan,

          You can use DesiredCapability class to set proxy for Chrome and other browsers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.