【发布时间】:2020-07-31 09:58:24
【问题描述】:
我在 python 项目中安装了 selenium 和 chrome 驱动程序,当我创建一个新项目时说它无法识别 selenium 模块。
这是否意味着每个项目我都需要再次安装 selenium?
【问题讨论】:
标签: python selenium selenium-webdriver module webdriver
我在 python 项目中安装了 selenium 和 chrome 驱动程序,当我创建一个新项目时说它无法识别 selenium 模块。
这是否意味着每个项目我都需要再次安装 selenium?
【问题讨论】:
标签: python selenium selenium-webdriver module webdriver
每次尝试运行自动化测试时,都需要在 selenium 中初始化浏览器。
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
当您尝试从不同的项目运行测试时,您不需要每次只需要初始化浏览器会话时安装 selenium。
也使用pip to install selenium 包。
安装 python 后尝试使用以下命令使用 pip 命令安装 selenium
pip install selenium
您将在Python\Lib\site-packages 下找到您的依赖项
【讨论】:
您不需要每次都需要,您需要通过提供路径和定位到您机器中的文件夹来使用
【讨论】:
是的,你需要。在您为每个项目创建虚拟环境的情况下。最好将其安装到项目虚拟环境中。这将为您在以后调试代码时节省大量压力。
【讨论】:
否,您不需要为每个项目安装Selenium。通过命令行界面使用以下任一命令,安装过程一生只有一次:
使用 pip:
pip install selenium
使用pip3:
pip3 install selenium
但是,您需要定期保持upgrading pip:
pip install -U pip
开启windows:
python -m pip install -U pip
或者
C:\Users\username>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 1.3MB 544kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0
作为一项强制性措施,您需要升级 Selenium 绑定以与每个 stable version 的发布保持同步,如下所示:
C:\Users\username>pip install -U selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/b8/53/9cafbb616d20c7624ff31bcabd82e5cc9823206267664e68aa8acdde4629/selenium-3.14.0-py2.py3-none-any.whl (898kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 901kB 380kB/s
Requirement not upgraded as not directly required: urllib3 in c:\python\lib\site-packages (from selenium) (1.22)
Installing collected packages: selenium
Found existing installation: selenium 3.12.0
Uninstalling selenium-3.12.0:
Successfully uninstalled selenium-3.12.0
Successfully installed selenium-3.14.0
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
您可以在以下位置找到详细的相关讨论:
【讨论】: