Python:没有名为selenium的模块

时间:2018-01-15 17:05:08

标签: python selenium

在网上搜索了几个小时后,我还没有找到问题的答案。我正在使用Python 3.6,我无法导入selenium。我总是收到消息“没有模块名为'selenium'' 我尝试了一切,我首先从这个网站https://pypi.python.org/pypi/selenium/3.6.0下载了硒。

然后我尝试了python -m pip install -U selenium并且也没有工作。我尝试了其他一些人说的但是他们也没有工作。 我正在使用Windows 10。 有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

正如您所说,using Python 3.6遵循以下步骤:

  • 打开Command Line InterfaceCLI)并发出命令python以检查Python是否已正确安装:

    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.
    >>>
    
  • 确保pip正常运行:

    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.
    
  • 通过selenium安装最新的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
    
  • 确认已安装Selenium

    C:\Users\username>pip freeze
    selenium==3.8.1 
    
  • 打开IDE(例如EclipsePyCharm)并编写一个简单的程序,如下所示:

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
    driver.get('https://stackoverflow.com')
    
  • 执行将启动Firefox Quantum浏览器的程序,并且将访问url https://stackoverflow.com

Python下载位置(Windows):

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

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

答案 1 :(得分: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 (包括硒文件夹)到第二个\site-packages ,希望能解决这个问题,对我来说很成功!

如何检查那里的位置

1)打开Python CLI,键入以下命令:

which python

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

>>> import site
>>> site.USER_SITE

编辑

由于这似乎是一个临时解决方案,所以我卸载了Python,然后将其重新安装在正确的目录(默认安装目录除外)中,现在which pythonwhich pip指向同一个文件夹!问题解决了!