pythonpython-3.xdjangoselenium-webdriverwebdriver

selenium: Max retries exceeded with url


Full traceback:

Traceback (most recent call last):
  File "/home/webadmin/dev.taracares.com/src/app_payroll_reports/tests.py", line 134, in test_mstnla_sums_incentives_tennessee_sum
    int(quince_amount_cell.text.replace(',', '')) +
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 77, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 423, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 333, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 355, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/request.py", line 74, in request
    return self.request_encode_url(
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/request.py", line 96, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/poolmanager.py", line 375, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/home/webadmin/dev.taracares.com/dev/lib/python3.8/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=58331): Max retries exceeded with url: /session/0f8192d1-c39f-49d1-9a99-7f91dfe95481/element/a5dd2645-6c15-44dc-b000-04bf6b7ea6cb/text (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd9dcde24f0>: Failed to establish a new connection: [Errno 111] Connection refused'))

This is code being run in tests for a django application. The tests used to run fine, now the last 4 tests (which use selenium) have errors, which started after I added driver.quit() after each test.

It's weird though. Here is the relevant code that runs which each (of the last four) tests:

    options = webdriver.FirefoxOptions()
    options.add_argument("--headless")

    driver = webdriver.Firefox(options=options)

    driver.get('https://dev.taracares.com/pr-run-ppe?group=MS%2FTN%2FLA&check_date=05%2F01%2F2022')

    # some selenium code

    driver.quit()

What is going on?


Solution

  • A line such as driver.quit() should be saved for a tearDown() method, etc. That way it still gets called if a test ends/fails early, and it doesn't get called too soon when you might still need the driver, such as when calling .text(), which requires the driver to still be open.

    Lots of existing frameworks for Python already have a tearDown() section where you can put driver.quit() (eg. classes that inherit unittest.TestCase), or it can be part of a pytest fixture, (eg. pytest_runtest_teardown). Some frameworks that combine Python/Selenium will automatically close the driver for you at the end of the test (eg. SeleniumBase), saving you that step. Full disclosure: I am the author/maintainer of SeleniumBase.