SeleniumHQ/selenium · error · Error::WebDriverError

too many redirects

Error message

too many redirects

What it means

Raised as Error::WebDriverError by Http::Default#follow_redirect when the redirect count reaches client_config.max_redirects. The Default adapter manually follows HTTP 3xx redirects by re-issuing GET requests to the Location header; each follow increments a counter and this error prevents infinite redirect loops.

Source

Thrown at rb/lib/selenium/webdriver/remote/http/default.rb:128

              sleep 2
              retry
            rescue Errno::ECONNREFUSED => e
              raise e.class, "using proxy: #{proxy.http}" if use_proxy?

              raise
            end

            if response.is_a? Net::HTTPRedirection
              follow_redirect(response, redirects)
            else
              WebDriver.logger.debug("   <<<  #{response.instance_variable_get(:@header).inspect}", id: :header)
              create_response response.code, response.body, response.content_type
            end
          end

          def follow_redirect(response, redirects)
            WebDriver.logger.debug("Redirect to #{response['Location']}; times: #{redirects}", id: :redirect)
            raise Error::WebDriverError, 'too many redirects' if redirects >= client_config.max_redirects

            request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)
          end

          def new_request_for(verb, url, headers, payload)
            req = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)

            req.basic_auth server_url.user, server_url.password if server_url.userinfo

            req.body = payload if payload

            req
          end

          def response_for(request)
            http.request request
          end

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Increase client_config.max_redirects if the redirect chain is legitimately long but finite.
  2. Diagnose the redirect loop: enable redirect logging (id: :redirect) to see each Location and the counter.
  3. Fix the server/proxy configuration causing the loop — often a trailing-slash or http/https scheme mismatch.
  4. Ensure the server_url has the correct scheme and path so the initial request doesn't trigger redirects.

Example fix

# before — default max_redirects too low or server has a loop
client = Selenium::WebDriver::Remote::Http::Default.new

# after — increase limit if chain is legitimate
config = Selenium::WebDriver::Remote::ClientConfig.new(server_url: url, max_redirects: 50)
client = Selenium::WebDriver::Remote::Http::Default.new(client_config: config)
# Diagnose loops with:
# Selenium::WebDriver.logger.level = :debug  # watch id: :redirect
Defensive patterns

Strategy: retry

Validate before calling

# Pre-flight: check the URL does not immediately redirect in a loop
require 'net/http'
uri = URI(server_url)
Net::HTTP.start(uri.host, uri.port) { |http| http.head(uri.path) }
# If this itself loops, fix the server/proxy before proceeding.

Try / catch

retries = 0
begin
  driver.navigate.to(url)
rescue Selenium::WebDriver::Error::WebDriverError => e
  raise unless e.message.include?('too many redirects')
  raise 'Redirect loop — fix server/proxy config' if retries >= 2
  retries += 1
  sleep 1
  retry
end

Prevention

When it happens

Trigger: The Selenium Server or an intermediate proxy responds with a redirect chain longer than max_redirects (default typically 20). A redirect loop (A -> B -> A) that never terminates. The server redirecting every request including the redirect target.

Common situations: Grid behind a reverse proxy with redirect loops due to trailing-slash or scheme (http/https) mismatches. Load balancer health-check redirects. Corporate proxies that redirect authentication. Server misconfiguration with http-to-https redirect loops when the client can't follow the scheme change.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/4ec98bde3f952014. Report an issue: GitHub.