tebeka / selenium

Selenium/Webdriver client for Go
MIT License
2.54k stars 409 forks source link

HTTP/SOCKS5 Proxy Not Working #220

Open austin-millan opened 3 years ago

austin-millan commented 3 years ago

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.

Abolah commented 3 years ago

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

Qingluan commented 3 years ago

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,
})
kasnet commented 3 years ago

@Qingluan It doesn't work

seamory commented 1 year ago

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

rudy-tao commented 1 year ago

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
}
ProDanceGrammer commented 1 year ago
  selenium.HTTPClient = ProxyClient()

What should be written at HTTP_PROXY?

ProDanceGrammer commented 1 year ago

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