0

I'm trying to run Selenium in headless mode, when using the code below it works fine.

proxy_url = get_random_proxy
proxy = Selenium::WebDriver::Proxy.new http: proxy_url, ssl: proxy_url

caps = Selenium::WebDriver::Options.chrome proxy: proxy

options = Selenium::WebDriver::Chrome::Options.new
# options.add_argument('--no-sandbox')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument('--disable-infobars')
options.add_argument('--disable-translate')
options.add_argument('--dns-prefetch-disable')
options.add_argument('--headless')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36')

prefs = { prompt_for_download: false,  default_directory: '/tmp' }
options.add_preference(:download, prefs)

driver = Selenium::WebDriver.for(:chrome, options: options, capabilities: [caps])
# driver = Selenium::WebDriver.for(:chrome, capabilities: [options, caps])
driver.devtools.new

driver.navigate.to "https://api.ipify.org"

But when replacing driver = Selenium::WebDriver.for(:chrome, options: options, capabilities: [caps]) with driver = Selenium::WebDriver.for(:chrome, capabilities: [options, caps]) it still opens the browser window.

This is the deprecation warning:

Selenium [DEPRECATION] [:browser_options] :options as a parameter for driver initialization is deprecated. Use :capabilities with an Array of value capabilities/options if necessary instead I'm trying to use the latter option because the first one is deprecated in Selenium 4

Redithion
  • 986
  • 1
  • 19
  • 32

1 Answers1

1

You are actually passing options twice (1 as options, 1 as capabilities) so it's getting confused. However, you can just pass proxy to the Chrome::Options and it should work correctly:

proxy_url = get_random_proxy
proxy = Selenium::WebDriver::Proxy.new http: proxy_url, ssl: proxy_url

options = Selenium::WebDriver::Options.chrome
options.proxy = proxy
# options.add_argument('--no-sandbox')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument('--disable-infobars')
options.add_argument('--disable-translate')
options.add_argument('--dns-prefetch-disable')
options.add_argument('--headless')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36')

prefs = { prompt_for_download: false,  default_directory: '/tmp' }
options.add_preference(:download, prefs)

driver = Selenium::WebDriver.for(:chrome, options: options, capabilities: caps)
# driver = Selenium::WebDriver.for(:chrome, capabilities: [options, caps])
driver.devtools.new

driver.navigate.to "https://api.ipify.org"
p0deje
  • 3,903
  • 1
  • 26
  • 37
  • Thanks, a few questions, will that work with proxy authentication? Also, oddly enough, if I use `capabilities: [caps, options]` then it works fine, any ideas why? – Redithion Jul 05 '22 at 18:14
  • 1
    Options are automatically converted to capabilities, so you can actually pass both of them and they will be merged. – p0deje Jul 06 '22 at 20:50
  • And yes, it should work with proxy authentication though it depends on authentication mechanism. – p0deje Jul 06 '22 at 20:53