Bootstrap

“ModuleNotFoundError: No module named ‘selenium‘”报错如何解决

接上节:测试平台开发之测试框架改造并发执行及结果隔离(1)

上节博客的末尾提到:在命令窗口执行python main.py
可是执行的时候遇到了如下报错:

============================================================================ ERRORS ============================================================================
_____________________________________________________________ ERROR collecting tests/test_case.py ______________________________________________________________
ImportError while importing test module 'D:\cekai\xuexi\Tesla\fullstack_framework\tests\test_case.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\.venv\Lib\site-packages\_pytest\python.py:487: in importtestmodule
    mod = import_path(
..\.venv\Lib\site-packages\_pytest\pathlib.py:591: in import_path
    importlib.import_module(module_name)
D:\python\Lib\importlib\__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
..\.venv\Lib\site-packages\_pytest\assertion\rewrite.py:178: in exec_module
    exec(co, module.__dict__)
tests\test_case.py:10: in <module>
    from commons.case_util import load_case, run_case
commons\case_util.py:12: in <module>
    from commons import settings
commons\settings.py:7: in <module>
    from selenium.webdriver.common.by import By
E   ModuleNotFoundError: No module named 'selenium'
======================================================================= warnings summary ======================================================================= 
..\.venv\Lib\site-packages\_pytest\config\__init__.py:1448
  D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\_pytest\config\__init__.py:1448: PytestConfigWarning: Unknown config option: result_log_verbose

    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

..\.venv\Lib\site-packages\_pytest\config\__init__.py:1448
  D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\_pytest\config\__init__.py:1448: PytestConfigWarning: Unknown config option: xlsx_meta_column_name

    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================================== short test summary info ==================================================================== 
ERROR tests/test_case.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
================================================================= 2 warnings, 1 error in 0.48s ================================================================= 
'allure' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

在这里插入图片描述
那么针对这个报错,要如何解决呢?

1. 模块导入错误

报错信息显示 selenium 模块未找到。你需要确保 selenium 安装在你的虚拟环境中。你可以通过以下步骤来解决:

激活你的虚拟环境(假设你的虚拟环境路径是 D:\cekai\xuexi\Tesla.venv):

D:\cekai\xuexi\Tesla\.venv\Scripts\activate

在这里插入图片描述

2、安装 selenium 模块:

pip install selenium

在这里插入图片描述
如果继续遇到报错:

ERROR: Exception:
Traceback (most recent call last):
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    yield
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read
    data = self._fp_read(amt) if not fp_closed else b""
           ^^^^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read
    return self._fp.read(amt) if amt is not None else self._fp.read()
           ^^^^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 98, in read
    data: bytes = self.__fp.read(amt)
                  ^^^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\http\client.py", line 479, in read
    s = self.fp.read(amt)
        ^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\socket.py", line 707, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\ssl.py", line 1253, in recv_into
    return self.read(nbytes, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\Lib\ssl.py", line 1105, in read
    return self._sslobj.read(len, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 180, in exc_logging_wrapper
             ^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 245, in wrapper
    return func(self, options, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\commands\install.py", line 377, in run
    requirement_set = resolver.resolve(
                      ^^^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 179, in resolve
    self.factory.preparer.prepare_linked_requirements_more(reqs)
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\operations\prepare.py", line 552, in prepare_linked_requirements_more
    self._complete_partial_requirements(
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\operations\prepare.py", line 467, in _complete_partial_requirements
    for link, (filepath, _) in batch_download:
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\network\download.py", line 183, in __call__
    for chunk in chunks:
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar
    for chunk in iterable:
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks
    for chunk in response.raw.stream(
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
    data = self.read(amt=amt, decode_content=decode_content)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read
    with self._error_catcher():
  File "D:\python\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "D:\cekai\xuexi\Tesla\.venv\Lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

两种方式解决:

1、使用国内的 PyPI 镜像源
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
2、设置超时
pip install selenium --timeout=120

3、验证 selenium 是否安装成功:

pip list

4、未知的配置选项警告

在 pytest.ini 文件中有两个未知的配置选项:result_log_verbose 和 xlsx_meta_column_name。这些选项不是 pytest 的标准配置选项,可能是由某些插件或自定义逻辑添加的。你可以通过以下步骤来解决:
1、检查 pytest.ini 文件:
打开 pytest.ini 文件并确认是否有以下内容:
在这里插入图片描述

5、allure 命令找不到

安装 allure 命令行工具:
你可以从 Allure2 官方 GitHub 下载适用于你系统的版本。
下载并解压后,将 bin 目录添加到系统的 PATH 环境变量中。

6、再次执行用例

 python main.py

可以看到用例运行成功
在这里插入图片描述

;