【问题标题】:No module named selenium even after installing it即使安装后也没有名为 selenium 的模块
【发布时间】:2016-10-27 15:59:48
【问题描述】:

我已经在 mac 上使用“pip install selenium”安装了 selenium。 还创建了一个文件 functional_tests.py 。它的内容是

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title

当我使用 python3 functional_tests.py 运行此脚本时,我得到:no module named selenium

【问题讨论】:

  • 试试pip3 install selenium。您的 pip 版本可能适用于 python 2.7
  • 谢谢..它成功了!

标签: python django python-3.x


【解决方案1】:

从 cmets 看来,您正在全局安装软件包。建议使用虚拟环境,以便您可以将包与每个项目紧密绑定。

这就是你在 Mac 上使用 Python 3 的方法:

$ cd <project_dir>
$ python3 -m venv venv # creates the directory ./venv
$ source venv/bin/activate
# you have now the virtual environment running using python3 and pip3
(venv)$ pip install -U pip setuptools
(venv)$ pip install selenium

对于您的测试,请考虑使用 Django 的 TestCase(Selenium 可能需要 LiveServerTestCase)和测试程序才能运行:

(venv)$ ./manage.py test  # this will run all of your tests

https://docs.djangoproject.com/en/1.10/topics/testing/overview/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 2019-11-07
    • 2020-09-17
    • 2021-06-28
    • 1970-01-01
    • 2018-08-18
    • 2022-07-06
    • 2023-03-27
    相关资源
    最近更新 更多