Cowsgo
Cowsgo

Reputation: 31

Change proxy after starting chromedriver in java selenium

Is it possible to use proxy after starting chromedriver on the same browser?

EX:

  1. I start chrome driver
  2. Load website
  3. Put in info
  4. Use proxy
  5. Click submit

I think i found some ways to do it in python and JS but im not sure how to convert it to java

Upvotes: 3

Views: 4382

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193308

As per Selenium's current implementation once you configure the WebDriver instance with the required Options and Capabilities and initialize the WebDriver session to open a Web Browser, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.

So, in-order to use a proxy you have to initiate a new WebDriver session.

here is @JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:

When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.

You can find a relevant discussion in Set capability on already running selenium webdriver

Upvotes: 3

Sugan
Sugan

Reputation: 455

You can use ChromeOptions class.

You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions 

object into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);



ChromeOptions options = new ChromeOptions();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);

// Add a ChromeDriver-specific capability.
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);

Upvotes: -1

Related Questions