Skip to main content
 首页 » 编程设计

python之没有名为 'selenium.webdriver' 的模块

2024年12月31日26lyhabc

我正在尝试使用 selenium 和 firefox 运行一些测试,我收到消息“ModuleNotFoundError:没有名为‘selenium.webdriver’的模块;‘selenium’不是一个包”

经过研究,我尝试了各种方法,但没有任何效果。我认为使用虚拟环境是最简单的方法,但我也无法做到这一 pip 。有什么我想念的吗?感谢任何输入。

[jim@localhost Environments]$ ll ~/.local/bin/ 
total 8528 
-rwxrwxr-x 1 jim jim     214 Sep  3 04:04 flask 
-rwxrwxr-x 1 jim jim 8724590 Jan 28 17:49 geckodriver 
 
[jim@localhost Environments]$ virtualenv -p python3.6 selenium 
Running virtualenv with interpreter /usr/bin/python3.6 
Using base prefix '/usr' 
New python executable in /home/jim/Environments/selenium/bin/python3.6 
Installing setuptools, pip, wheel...done. 
 
[jim@localhost Environments]$ source selenium/bin/activate 
 
(selenium) [jim@localhost Environments]$ which python 
~/Environments/selenium/bin/python 
 
(selenium) [jim@localhost Environments]$ ~/Environments/selenium/bin/python --version 
Python 3.6.4 
 
(selenium) [jim@localhost Environments]$ pip install selenium 
Collecting selenium  
(from selenium) (1.24.1) 
Installing collected packages: selenium 
Successfully installed selenium-3.141.0 
 
(selenium) [jim@localhost Environments]$ pip freeze 
selenium==3.141.0 
urllib3==1.24.1 
 
(selenium) [jim@localhost Environments]$ pip show selenium 
Name: selenium 
Version: 3.141.0 
Summary: Python bindings for Selenium 
Home-page: https://github.com/SeleniumHQ/selenium/ 
Author: UNKNOWN 
Author-email: UNKNOWN 
License: Apache 2.0 
Location: /home/jim/Environments/selenium/lib/python3.6/site-packages 
Requires: urllib3 
Required-by: 
 
(selenium) [jim@localhost Environments]$ ~/Environments/selenium/bin/python 
Python 3.6.4 (default, Dec 19 2017, 14:48:12) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from selenium.webdriver import Firefox 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "/home/jim/Environments/selenium.py", line 1, in <module> 
    from selenium.webdriver import Firefox 
ModuleNotFoundError: No module named 'selenium.webdriver'; 'selenium' is not  
a package 

请您参考如下方法:

正如我和@buran 所评论的那样:

你有一个名为 selenium 的文件夹下的 python venv,它混合了导入......

正如您在 /home/jim/Environments/selenium/bin/python3.6 中显示的那样,请参阅保存 python 的 bin 文件夹名为 selenium 改变它,它应该可以正常工作!

您可以将其更改为类似 new_selenium 的内容:/home/jim/Environments/new_selenium/bin/python3.6

希望对你有帮助!