ruby-on-rails - 如何在 headless Chrome 中进行代理身份验证

标签 ruby-on-rails ruby proxy webdriver google-chrome-headless

我正在尝试通过需要身份验证的代理使用 headless Chrome 浏览器:

require "selenium-webdriver"

options = Selenium::WebDriver::Chrome::Options.new(
      args: ["headless", "proxy-server=http://#{host}:#{port}"]
    )
driver = Selenium::WebDriver.for(:chrome, options: options)

这在不需要身份验证时有效,但现在我需要通过身份验证使用它。

在“List of Chromium Command Line Switches”中我没有找到如何通过usernamepassword正确。在谷歌我找到了这个选项,但它不起作用:
options = Selenium::WebDriver::Chrome::Options.new(
      args: ["headless", "proxy-server=http://#{username}:#{password}@#{host}:#{port}"]
    )

如何通过代理身份验证使用 headless-chrome?

最佳答案

我一直在寻找解决方案近 2 周。那是在 2019 年,我什至没有弄清楚。据我所知,您不能使用用户名/密码设置代理。以前可以使用扩展,但现在这种方式不可用。有一种方法可以使用 webdrivers gem :

如果您和 Internet 之间有代理,那么您需要配置 gem 以使用代理。您可以通过调用 configure 方法来做到这一点。

Webdrivers.configure do |config|
  config.proxy_addr = 'myproxy_address.com'
  config.proxy_port = '8080'
  config.proxy_user = 'username'
  config.proxy_pass = 'password'
end

但是我无法让它在我的项目中工作。唯一有效的是在代理服务器上使用我的 IP 设置白名单,因此我不需要设置用户名/密码。所以它不需要凭据就可以工作,就像这样:

    Capybara.register_driver :headless_chrome do |app|
      client = Selenium::WebDriver::Remote::Http::Default.new
      client.read_timeout = 60
      capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
        'goog:chromeOptions' => { args: %w[no-sandbox headless disable-gpu disable-dev-shm-usage
                                           window-size=1280,1024 enable-features=NetworkService,NetworkServiceInProcess] },
        'loggingPrefs' => { browser: 'ALL', client: 'ALL', driver: 'ALL', server: 'ALL' }
      )
      capabilities['goog:chromeOptions'][:args] << "user-agent=#{user_agent}" if user_agent
      capabilities['goog:chromeOptions'][:args] << "proxy-server=http://#{proxy[:ip]}:#{proxy[:port]}" if proxy

      Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities, http_client: client)
    end

我知道我的答案不是您一直在寻找的,但也许它会以某种方式帮助您或促使您正确答案。祝你好运

关于ruby-on-rails - 如何在 headless Chrome 中进行代理身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397637/

相关文章:

delphi - IdHTTP + socks + socks 5

mysql - CryptDB - 无法连接到代理(错误 1105 (HY000) : (proxy) all backends are down)

http - http代理如何工作?

javascript - 使用 JS 无需刷新或重新加载页面

ruby-on-rails - 数据库连接错误?使用 Postgres

ruby-on-rails - 为什么rails会在controller中生成对象实例变量

ruby-on-rails - 将大型 Rails 应用程序分解成较小的应用程序?

ruby-on-rails - 如何将 Ruby on Rails 网站预编译为生产模式?

ruby-on-rails - 在 yaml 文件中传递变量

css - .fields_with_errors 的 Bootstrap 样式不起作用