• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Handle Proxy in Selenium Webdriver

How to Handle Proxy in Selenium Webdriver

March 18, 2015 by Mukesh Otwani 6 Comments

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 🙂

Filed Under: Advance Selenium Tagged With: Proxy in Selenium

Reader Interactions

Comments

  1. manoj says

    January 19, 2017 at 11:12 AM

    I am using this code but not working!!

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:54 PM

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

      Reply
  2. S. Shaikh says

    June 28, 2016 at 10:03 AM

    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

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:07 PM

      Hey Shaikh,

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

      Reply
      • Rajan singh says

        November 10, 2016 at 12:33 PM

        How to set proxy in chrome

        Reply
        • Mukesh Otwani says

          November 10, 2016 at 4:16 PM

          Hey Ranjan,

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

          Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?