Python: 没有名为selenium的模块

3
在网上搜索了数小时后,我仍然没有找到解决我的问题的答案。我正在使用Python 3.6,但无法导入selenium。我总是收到“没有名为'selenium'的模块”的消息。 我尝试了所有方法,首先从此网站https://pypi.python.org/pypi/selenium/3.6.0下载了selenium 3.6.0版本,但失败了。然后我尝试了python -m pip install -U selenium,但也不起作用。我尝试了其他一些人提供的方法,但它们都没有起作用。 我正在使用Windows 10。 有什么建议吗?

安装Python的Selenium时遇到了问题。 - GalAbra
3
可能是 Trouble installing selenium for python 的重复问题。 - GalAbra
https://gist.github.com/baijum/1047207/33b3ac998e30e967318b14dc1db1f1d687c34d5d 上说:“Python 3尚未得到支持。” - Mark K
4个回答

2

正如您所提到的,您正在使用Python 3.6,按照以下步骤:

  • Open Command Line Interface (CLI) and issue the command python to check if Python is properly installed :

    C:\Users\username>python
    Python 3.6.1 (v3.6.1:69c0db5, Jan 16 2018, 17:54:52) [MSC v.1900 32 bit (Intel)]
     on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  • Ensure pip is working properly :

    C:\Users\username>pip
    
    Usage:
      pip <command> [options]
    
    Commands:
      install                     Install packages.
      download                    Download packages.
      uninstall                   Uninstall packages.
      freeze                      Output installed packages in requirements format.
      list                        List installed packages.
      show                        Show information about installed packages.
      check                       Verify installed packages have compatible dependencies.
      search                      Search PyPI for packages.
      wheel                       Build wheels from your requirements.
      hash                        Compute hashes of package archives.
      completion                  A helper command used for command completion.
      help                        Show help for commands.
    
    General Options:
      -h, --help                  Show help.
      --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
      -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
      -V, --version               Show version and exit.
      -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
      --log <path>                Path to a verbose appending log.
      --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
      --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
      --timeout <sec>             Set the socket timeout (default 15 seconds).
      --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
      --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
      --cert <path>               Path to alternate CA bundle.
      --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
      --cache-dir <dir>           Store the cache data in <dir>.
      --no-cache-dir              Disable the cache.
      --disable-pip-version-check
                                  Don't periodically check PyPI to determine
                                  whether a new version of pip is available for
                          download. Implied with --no-index.
    
  • Install latest selenium through pip :

    C:\Users\username>pip install -U selenium
    Collecting selenium
      Downloading selenium-3.8.1-py2.py3-none-any.whl (931kB)
        100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 942kB 322kB/s
    Installing collected packages: selenium
    Successfully installed selenium-3.8.1
    
  • Confirm that Selenium is installed :

    C:\Users\username>pip freeze
    selenium==3.8.1 
    
  • Open an IDE (e.g Eclipse, PyCharm) and write a simple program as follows :

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
    driver.get('https://stackoverflow.com')
    
  • Execute the program on which Firefox Quantum Browser will be initiated and the url https://stackoverflow.com will be accessed.

Python下载地址(Windows):

可以从以下位置下载Python(适用于Windows):

https://www.python.org/downloads/

1
首先,感谢您的回复。我已经检查了Python并且它似乎已经正确安装了。然而,pip在命令行中却不能识别为内部或外部命令。我尝试通过设置环境变量来解决问题,但是并没有起作用。所以问题出在pip身上。 - Jordan
通过 Revo Uninstaller 卸载 Python 并安装最新的 GA 版本 Python - undetected Selenium
1
好的,我已经完成了。您能否建议我应该下载什么? - Jordan
我更新了我的回答,并提供了下载位置。请下载并安装最新的Python GA版本。 - undetected Selenium

1
我在Windows 10上使用VS Code,以下是我的解决方法。
你需要注意Python所在的位置(以我的情况为例)。
1) C:\Users\_Me_\AppData\Local\Programs\Python\Python38\ 

Python会搜索库/包的位置,包括使用pip(在我的情况下)安装的库/包。

2) C:\Users\_Me_\AppData\Roaming\Python\Python38\

我不知道这两个位置为什么不同(必须在某个时间点进行修复)。看起来 Python 是从第一个位置运行的,但它在第二个位置寻找库!:/ 无论如何,由于我对 Python 经验有限,所以我只是将第一个位置中的 \Lib\site-packages(包括 selenium 文件夹)复制到第二个位置的 site-packages 中,希望解决问题,结果对我有效!
如何检查这些位置:
1)打开 Python CLI,输入以下命令:
which python

2)打开Python CLI,输入以下命令(来自this answer):

>>> import site
>>> site.USER_SITE

编辑

由于这似乎是一个临时解决方案,我卸载了Python,并在一个适当的目录(而不是默认的安装目录)重新安装了它,现在which pythonwhich pip指向同一个文件夹!问题解决了!


这个问题很老,但它可能会帮助一些人,所以感谢你的回答。 - Jordan

0
如果这些解决方案对您无效,请尝试查看您的IDE正在使用哪个Python解释器(在我的情况下,我正在使用VSCode - 您可以在底部栏中找到解释器)。 对我有用。

1
目前你的回答不太清晰,请[编辑]并添加更多细节,以帮助他人理解其如何解决所问问题。您可以在帮助中心找到有关编写好答案的更多信息。 - Community

0

最简单的方法是将“venv”Lib、Scripts、Selenium和其他文件夹的所有文件复制到您的主项目文件夹中。

这个问题是因为PyCharm直接从虚拟环境venv中获取。

希望这能解决您的问题 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接