I'm trying to get selenium make request through a proxy server but I can't seem it get it working.
This bit of code gets a response but isn't using the proxy
Selenium::WebDriver.logger.level = :info
proxy = Selenium::WebDriver::Proxy.new(
:http => "http://username:password@host:port",
:ssl => "http://username:password@host:port"
)
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
proxy: proxy,
'goog:chromeOptions' => {
'args' => ['headless', 'no-sandbox', 'disable-gpu', 'window-size=1920,1080']
}
)
driver = Selenium::WebDriver.for(:chrome, :capabilities => caps)
driver.navigate.to "https://www.showmyip.com/"
2021-06-21 12:51:16 INFO Selenium -> POST session
2021-06-21 12:51:16 INFO Selenium >>> http://127.0.0.1:9515/session | {"capabilities":{"alwaysMatch":{"proxy":{"proxyType":"manual","httpProxy":"http://username:password@host:port","sslProxy":"http://username:password@host:port"},"browserName":"chrome","goog:chromeOptions":{"args":["headless","no-sandbox","disable-gpu","window-size=1920,1080"]}}}}
2021-06-21 12:51:16 INFO Selenium <- {"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"chrome","browserVersion":"91.0.4472.114","chrome":{"chromedriverVersion":"91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462})","userDataDir":"/tmp/.com.google.Chrome.8Qfzwu"},"goog:chromeOptions":{"debuggerAddress":"localhost:46057"},"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platformName":"linux","proxy":{"httpProxy":"http://username:password@host:port","proxyType":"manual","sslProxy":"http://username:password@host:port"},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","webauthn:extension:largeBlob":true,"webauthn:virtualAuthenticators":true},"sessionId":"b5a74fa8a44d298fb1fbffcc0516df83"}}
2021-06-21 12:51:16 INFO Selenium -> POST session/b5a74fa8a44d298fb1fbffcc0516df83/url
2021-06-21 12:51:16 INFO Selenium >>> http://127.0.0.1:9515/session/b5a74fa8a44d298fb1fbffcc0516df83/url | {"url":"https://www.showmyip.com/"}
2021-06-21 12:51:18 INFO Selenium <- {"value":null}
Removing the scheme from the proxy string results in the same issue
2021-06-21 14:47:24 INFO Selenium >>> http://127.0.0.1:9515/session | {"capabilities":{"alwaysMatch":{"proxy":{"proxyType":"manual","httpProxy":"username:password@host:port","sslProxy":"username:password@host:port"},"browserName":"chrome","goog:chromeOptions":{"args":["headless","no-sandbox","disable-gpu","window-size=1920,1080"]}}}}
2021-06-21 14:47:24 INFO Selenium <- {"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"chrome","browserVersion":"91.0.4472.114","chrome":{"chromedriverVersion":"91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462})","userDataDir":"/tmp/.com.google.Chrome.aVNdyH"},"goog:chromeOptions":{"debuggerAddress":"localhost:33363"},"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platformName":"linux","proxy":{"httpProxy":"username:password@host:port","proxyType":"manual","sslProxy":"username:password@host:port"},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","webauthn:extension:largeBlob":true,"webauthn:virtualAuthenticators":true},"sessionId":"3d95da424ededc7f0a9f59dd8e20386d"}}
2021-06-21 14:47:24 INFO Selenium -> POST session/3d95da424ededc7f0a9f59dd8e20386d/url
2021-06-21 14:47:24 INFO Selenium >>> http://127.0.0.1:9515/session/3d95da424ededc7f0a9f59dd8e20386d/url | {"url":"https://www.showmyip.com/"}
2021-06-21 14:47:25 INFO Selenium <- {"value":null}
I've also tried this approach
client = Selenium::WebDriver::Remote::Http::Default.new
client.proxy = Selenium::WebDriver::Proxy.new(
:http => "http://username:password@host:port",
:ssl => "http://username:password@host:port"
)
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions' => {
'args' => ['headless', 'no-sandbox', 'disable-gpu', 'window-size=1920,1080']
}
)
driver = Selenium::WebDriver.for(:remote, :http_client => client, :capabilities => caps)
driver.navigate.to "https://www.showmyip.com/"
Which gives me the following 502 error. The proxies I'm using work with Net/Http so I don't believe they are the problem
/home/will/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.0.beta4/lib/selenium/webdriver/remote/http/common.rb:93:in `create_response': unexpected response, code=502, content-type="text/html" (Selenium::WebDriver::Error::WebDriverError)
<html><head><title>502 Bad Gateway</title></head>\r
<body><h2>502 Bad Gateway</h2><h3>Host Not Found or connection failed</h3></body></html>
If I enter the wrong username or password intentionally using the same code I get this error
/home/will/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.0.beta4/lib/selenium/webdriver/remote/http/common.rb:93:in `create_response': unexpected response, code=407, content-type="text/html" (Selenium::WebDriver::Error::WebDriverError)
<html><head><title>407 Proxy Authentication Required</title></head>\r
<body><h2>407 Proxy Authentication Required</h2><h3>Access to requested resource disallowed by administrator or you need valid username/password to use this resource</h3></body></html>
Update
After further digging it appears that selenium is ignoring proxies with auth.
Using proxy_chain_rb to setup a proxy on localhost without authentication that redirects to your actual proxy seems to work.
This is not an ideal solution, I plan on using a different proxy for each request and as far as I can tell I will need to start the proxy server on each request which takes a few seconds.
real_proxy = "http://username:password@host:port"
server = ProxyChainRb::Server.new
generated_proxy = server.start(real_proxy)
proxy = {
http: generated_proxy,
ssl: generated_proxy
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
proxy: proxy,
'goog:chromeOptions' => {
'args' => ['headless', 'no-sandbox', 'disable-gpu', 'window-size=1920,1080']
}
)
driver = Selenium::WebDriver.for(:chrome, :capabilities => caps)
driver.navigate.to "https://www.showmyip.com/"