selenium:url 超出最大重试次数

selenium: Max retries exceeded with url

完整追溯:

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'))

这是 运行 在 Django 应用程序测试中的代码。过去的测试 运行 很好,现在最后 4 个测试(使用 selenium)有错误,这是在我在每次测试后添加 driver.quit() 之后开始的。

虽然很奇怪。这是 运行s 的相关代码,每个(最后四个)测试:

    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()

这是怎么回事?

确保 quit 实际上位于代码的底部。出错的命令是获取元素的文本。您似乎已经退出了驱动程序,然后尝试调用以获取与该驱动程序会话关联的元素的文本。

应该为 tearDown() 方法等保存诸如 driver.quit() 的行。这样,如果测试 ends/fails 早,它仍然会被调用,并且不会被调用当您可能仍需要驱动程序时调用得太早,例如调用 .text() 时,这需要驱动程序仍处于打开状态。

许多 Python 的现有框架已经有一个 tearDown() 部分,您可以在其中放置 driver.quit()(例如 类 继承 unittest.TestCase),或者它可以是 pytest 夹具的一部分,(例如 pytest_runtest_teardown)。某些结合 Python/Selenium 的框架会在测试结束时自动为您关闭驱动程序(例如 SeleniumBase),从而为您省去这一步。全面披露:我是 SeleniumBase 的 author/maintainer。