完整追溯:
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()
后开始的。
不过这很奇怪。以下是运行每个(最后四个)测试的相关代码:
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 实际上位于代码的底部。出错的命令是获取元素的文本。您似乎已退出驱动程序,然后尝试调用以获取与该驱动程序会话关联的元素的文本。
应为
driver.quit()
方法等保存诸如 tearDown()
之类的行。这样,如果测试提前结束/失败,它仍然会被调用,并且当您可能仍然需要驱动程序时,它不会被太快调用,比如打电话.text()
时,需要司机仍然开着。
许多现有的 Python 框架已经有一个
tearDown()
部分,您可以在其中放置 driver.quit()
(例如继承 unittest.TestCase
的类),或者它可以是 pytest
固定装置的一部分(例如 pytest_runtest_teardown
) )。一些结合 Python/Selenium 的框架会在测试结束时自动为您关闭驱动程序(例如 SeleniumBase),从而节省您这一步。完全披露:我是 SeleniumBase 的作者/维护者。