今天在Pycharm中写了一段登录的脚本,然后使用Terminal运行,结果就报错,但是右键直接Run是正常的,先来看一下报错信息:
test_cases/test_login.py::TestLogin::test_login_fun FAILED
=============================================================== FAILURES ================================================================
_______________________________________________________ TestLogin.test_login_fun ________________________________________________________self = <test_cases.test_login.TestLogin object at 0x000001C4E5355300>
def test_login_fun(self):
# 打开Firefox浏览器
# option = webdriver.ChromeOptions()
# option.add_experimental_option("detach", True)
# driver = webdriver.Chrome(options=option)
> driver = webdriver.Firefox()test_cases\test_login.py:10:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\firefox\webdriver.py:170: in __init__
RemoteWebDriver.__init__(
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py:157: in __init__
self.start_session(capabilities, browser_profile)
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py:252: in start_session
response = self.execute(Command.NEW_SESSION, parameters)
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py:319: in execute
response = self.command_executor.execute(driver_command, params)
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\remote_connection.py:376: in execute
return self._request(command_info[0], url, body=data)
D:\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\remote_connection.py:399: in _request
resp = self._conn.request(method, url, body=body, headers=headers)
D:\Programs\Python\Python310\lib\site-packages\urllib3\_request_methods.py:144: in request
return self.request_encode_body(
D:\Programs\Python\Python310\lib\site-packages\urllib3\_request_methods.py:279: in request_encode_body
return self.urlopen(method, url, **extra_kw)
D:\Programs\Python\Python310\lib\site-packages\urllib3\poolmanager.py:433: in urlopen
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
D:\Programs\Python\Python310\lib\site-packages\urllib3\poolmanager.py:304: in connection_from_host
return self.connection_from_context(request_context)
D:\Programs\Python\Python310\lib\site-packages\urllib3\poolmanager.py:329: in connection_from_context
return self.connection_from_pool_key(pool_key, request_context=request_context)
D:\Programs\Python\Python310\lib\site-packages\urllib3\poolmanager.py:352: in connection_from_pool_key
pool = self._new_pool(scheme, host, port, request_context=request_context)
D:\Programs\Python\Python310\lib\site-packages\urllib3\poolmanager.py:266: in _new_pool
return pool_cls(host, port, **request_context)
D:\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py:196: in __init__
timeout = Timeout.from_float(timeout)
D:\Programs\Python\Python310\lib\site-packages\urllib3\util\timeout.py:186: in from_float
return Timeout(read=timeout, connect=timeout)
D:\Programs\Python\Python310\lib\site-packages\urllib3\util\timeout.py:115: in __init__
self._connect = self._validate_timeout(connect, "connect")
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _cls = <class 'urllib3.util.timeout.Timeout'>, value = <object object at 0x000001C4E2244E60>, name = 'connect'
@classmethod
def _validate_timeout(cls, value: _TYPE_TIMEOUT, name: str) -> _TYPE_TIMEOUT:
"""Check that a timeout attribute is valid.:param value: The timeout value to validate
:param name: The name of the timeout attribute to validate. This is
used to specify in error messages.
:return: The validated and casted version of the given value.
:raises ValueError: If it is a numeric value less than or equal to
zero, or the type is not an integer, float, or None.
"""
if value is None or value is _DEFAULT_TIMEOUT:
return valueif isinstance(value, bool):
raise ValueError(
"Timeout cannot be a boolean value. It must "
"be an int, float or None."
)
try:
float(value)
except (TypeError, ValueError):
> raise ValueError(
"Timeout value %s was %s, but it must be an "
"int, float or None." % (name, value)
) from None
E ValueError: Timeout value connect was <object object at 0x000001C4E2244E60>, but it must be an int, float or None.D:\Programs\Python\Python310\lib\site-packages\urllib3\util\timeout.py:152: ValueError
======================================================== short test summary info ========================================================
FAILED test_cases/test_login.py::TestLogin::test_login_fun - ValueError: Timeout value connect was <object object at 0x000001C4E2244E60...
=========================================================== 1 failed in 3.35s ===========================================================
先分析一下原因,首先右键Run是正常的,可以先排除脚本问题,考虑一下这两种运行方式的不同,怀疑是不是某个运行库的原因,看看报错信息中提到了urllib3,先看一下该项目中使用的selenium和urllib3的版本,
因为Terminal属于终端运行,这个时候突然想到了DOS中的这个selenium和urllib3版本是不是不匹配,查看一下:
可以看出来,DOS中的selenium版本是3.x的版本,和urllib3不匹配,所以尝试将DOS中的selenium版本升到4.x版本,先用命令行pip uninstall selenium卸载旧版本,然后使用pip install selenium==4.1升级到新版本,升级后再查看如下图:
再回到Pycharm,在Terminal中运行pytest,
OK,搞定了!