13

I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.

I tried below code, but still it shows the same error:

Proxy p=new Proxy();


// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");

// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();

// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);

// Open  firefox browser
WebDriver driver=new ChromeDriver(cap);
Eugene S
  • 6,709
  • 8
  • 57
  • 91
Praveen Medipally
  • 209
  • 1
  • 2
  • 7

7 Answers7

11

Passing a Capabilities object to the ChromeDriver() constructor is deprecated. One way to use a proxy is this:

String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);
user3052604
  • 136
  • 1
  • 7
7

Issue is resolved with below code -

Proxy proxy = new Proxy(); 
proxy.setHttpProxy("yoururl:portno"); 
proxy.setSslProxy("yoururl:portno"); 

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
capabilities.setCapability("proxy", proxy); 

ChromeOptions options = new ChromeOptions(); 
options.addArguments("start-maximized"); 

capabilities.setCapability(ChromeOptions.CAPABILITY, options); 

driver = new ChromeDriver(capabilities);
fllprbt
  • 103
  • 1
  • 10
Praveen Medipally
  • 209
  • 1
  • 2
  • 7
  • Hi @Praveen Medipally, I am getting "This site can’t be reached" error in browser with above code. I am having user id and password for my proxy server. I just injected it in my url like http://user:pwd@proxy:port. Is above code still working for you? – Solomon Raja Jun 14 '18 at 12:42
  • 4
    Passing a Capabilities object to the ChromeDriver() constructor is deprecated. – Stéphane GRILLON May 13 '19 at 09:57
7

Passing a Capabilities object to the ChromeDriver() constructor is deprecated. You can find new official doc here.

ChromeOptions chromeOptions = new ChromeOptions();

Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port"); 
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");

chromeOptions.setCapability("proxy", proxy); 
driver = new ChromeDriver(chromeOptions);
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
4
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();              
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");                      
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);
Barney
  • 1,851
  • 2
  • 19
  • 36
3

Another way of doing it:

        boolean useProxy = true;
        ChromeOptions options = new ChromeOptions().addArguments(
                '--headless',
                '--no-sandbox',
                '--disable-extensions',
                '--proxy-bypass-list=localhost');
        if (useProxy) {
            options.addArguments("--proxy-server=http://ProxyHost:8080");
        }

        WebDriver driver = new ChromeDriver(options);

See https://peter.sh/experiments/chromium-command-line-switches/ for more Chrome switches

Slavik
  • 1,488
  • 1
  • 15
  • 24
0

Another way to set a proxy:

    Proxy proxy = new Proxy();
    proxy.setHttpProxy("<HOST:PORT>");
    proxy.setSslProxy("<HOST:PORT>");

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setCapability("proxy", proxy);

    WebDriver webDriver = new ChromeDriver(chromeOptions);
0

The only way I could use proxy was like this

String proxy="proxy-server-ip";
WebDriverManager.chromedriver().proxy(proxy).setup();
Gerrie
  • 1
  • 1