Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set proxy for Chrome browser in selenium using Java code

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);
like image 571
Praveen Medipally Avatar asked Jul 24 '17 19:07

Praveen Medipally


People also ask

How do I set Chrome as my proxy?

Open Google Chrome, click on the Settings icon, and Options. Select Under the hood, then Change proxy settings… Select Connections and LAN Settings. Select “Automatically detect settings”, and click OK.

Which of the following codes is used in selenium to configure the use of proxy?

Following piece of code used to set proxy in Selenium. ChromeOptions option = new ChromeOptions(); Proxy proxy = new Proxy(); proxy. setHttpProxy("localhost:5555"); option. setCapability(CapabilityType.


1 Answers

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);
like image 153
Praveen Medipally Avatar answered Oct 27 '22 01:10

Praveen Medipally