selenium icon indicating copy to clipboard operation
selenium copied to clipboard

HTTP/SOCKS5 Proxy Not Working

Open austin-millan opened this issue 5 years ago • 7 comments

Version Info

Selenium server version: 3.141.59
Mozilla version: 83.0
Geckodriver version: 0.28.0

Using only HTTP field

This configuration uses only the HTTP field of the selenium.Proxy struct. When creating a new selenium service with this configuration it does not run into errors, but the proxy isn't actually used in requests.

Capabilities are: {
  "browserName": "firefox",
  "moz:firefoxOptions": {
    "args": [
      "--user-agent=Mozilla\u002f5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\u002f537.36 (KHTML, like Gecko) Chrome\u002f84.0.4147.135 Safari\u002f537.36"
    ]
  },
  "proxy": {
    "httpProxy": "<REDACTED_IP>:80",
    "proxyType": "manual"
  }

Using HTTP field and port

Specifying the HTTP port and IP (separately) causes error when creating new remote:

15:41:52.786 INFO [ActiveSessionFactory.apply] - Capabilities are: {
  "browserName": "firefox",
  "moz:firefoxOptions": {
    "args": [
      "--user-agent=Mozilla\u002f5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\u002f537.36 (KHTML, like Gecko) Chrome\u002f84.0.4147.135 Safari\u002f537.36"
    ]
  },
  "proxy": {
    "httpProxy": "<REDACTED_IP>",
    "httpProxyPort": 80,
    "proxyType": "manual"
  }
}
...
org.openqa.selenium.InvalidArgumentException: Invalid proxy configuration entry: httpProxyPort

SOCKS5

Note the same things happen with SOCKS5 Proxy configuration, except there's also an issue when specifying the proxy.SOCKSVersion, something about casting a long to an int in Java.

austin-millan avatar Nov 22 '20 23:11 austin-millan

Heyo, I'm encountering the same issues with a chromedriver browser. did you find a fix ?

Abolah avatar Jan 25 '21 21:01 Abolah

Author is baka!

// this is work for me when using Chromedriver
caps.AddProxy(selenium.Proxy{
	Type: selenium.Manual,
	SOCKS: "localhost:1080", // this field should include port!!
	// SocksPort:    port,   DO NOT SET THIS FIELD
	SOCKSVersion: 5,
})

Qingluan avatar Feb 03 '21 02:02 Qingluan

@Qingluan It doesn't work

kasnet avatar Apr 24 '21 01:04 kasnet

it's working for me when using chromedriver. thx.

seamory avatar Nov 25 '22 07:11 seamory

try setting selenium .HTTPClient. fix!

func NewRemote(ctx context.Context) (selenium.WebDriver, error) {
	caps := selenium.Capabilities{"browserName": "chrome"}
	var args []string
	args = append(args, "--no-sandbox")
	caps.AddChrome(chrome.Capabilities{
		Args:            args,
	})
	selenium.HTTPClient = ProxyClient()
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 8080))
	if err != nil {
		return nil, err
	}
	return wd, err
}

func ProxyClient() *http.Client {
	httpProxy := os.Getenv("HTTP_PROXY")
	if httpProxy == "" {
		return &http.Client{}
	}
	proxyUrl, err := url.Parse(httpProxy)
	if err != nil {
		return &http.Client{}
	}
	client := &http.Client{Transport: &http.Transport{
		Proxy: http.ProxyURL(proxyUrl),
	}}
	return client
}

rudy-tao avatar Jan 16 '23 08:01 rudy-tao

	selenium.HTTPClient = ProxyClient()

What should be written at HTTP_PROXY?

ProDanceGrammer avatar Jun 12 '23 18:06 ProDanceGrammer

try setting selenium .HTTPClient. fix!

func NewRemote(ctx context.Context) (selenium.WebDriver, error) {
	caps := selenium.Capabilities{"browserName": "chrome"}
	var args []string
	args = append(args, "--no-sandbox")
	caps.AddChrome(chrome.Capabilities{
		Args:            args,
	})
	selenium.HTTPClient = ProxyClient()
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 8080))
	if err != nil {
		return nil, err
	}
	return wd, err
}

func ProxyClient() *http.Client {
	httpProxy := os.Getenv("HTTP_PROXY")
	if httpProxy == "" {
		return &http.Client{}
	}
	proxyUrl, err := url.Parse(httpProxy)
	if err != nil {
		return &http.Client{}
	}
	client := &http.Client{Transport: &http.Transport{
		Proxy: http.ProxyURL(proxyUrl),
	}}
	return client
}

@rudy-tao it doesn't work

ProDanceGrammer avatar Jun 12 '23 18:06 ProDanceGrammer