解决:ModuleNotFoundError: No module named ‘selenium’


解决:ModuleNotFoundError: No module named ‘selenium’



文章目录

  • 解决:ModuleNotFoundError: No module named 'selenium'
    • 背景
    • 报错问题
    • 报错翻译
    • 报错位置代码
    • 报错原因
    • 解决方法
      • 方法一,直接安装
      • 方法二,手动下载安装
      • 方法三,编译安装
    • Selenium 简介
      • 今天的分享就到此结束了



背景

在使用之前的代码时,报错:
Traceback (most recent call last):
File “xxx”, line xx, in
import selenium
ModuleNotFoundError: No module named ‘selenium’



报错问题


  Traceback (most recent call last): 
    File "xxx", line xx, in  
          import selenium 
  ModuleNotFoundError: No module named 'selenium'



报错翻译

主要报错信息内容翻译如下所示:


  Traceback (most recent call last): 
    File "xxx", line xx, in  
          import selenium 
  ModuleNotFoundError: No module named 'selenium'

翻译:

追溯(最近一次通话):
文件“xxx”,第xx行,在
导入selenium
ModuleNotFoundError:没有名为“selenium”的模块



报错位置代码


...
      import selenium 
...



报错原因

经过查阅资料,发现这个错误通常是由于缺少selenium的依赖库,就会出现这样的提示。

小伙伴们按下面的解决方法即可解决!!!



解决方法

要解决这个错误,需要安装selenium的依赖库,即可解决。

方法一,直接安装

在window下安装selenium

pip install selenium

如果你嫌下载慢,可以更改下载源,

比如,利用豆瓣的下载源

pip install selenium -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

附,国内各大源列表:

名称 地址
阿里 https://mirrors.aliyun.com/pypi/simple
豆瓣 http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple
华中理工大学 http://pypi.hustunique.com/simple
山东理工大学 http://pypi.sdutlinux.org/simple
网易 https://mirrors.163.com/pypi/simple/
腾讯 https://mirrors.cloud.tencent.com/pypi/simple

如果你使用的是conda环境,请尝试使用conda install安装selenium。

如果你使用的是pip安装selenium,可以尝试使用以下命令安装:

pip install --no-cache-dir selenium

方法二,手动下载安装

将安装包手动下载到本地进行安装,然后在cmd 中进入安装包的路径下面用:cd +路径,进入。

然后输入以下命令:

pip install selenium-4.17.2-py3-none-any.whl 

就可以进行安装了。

附下载链接,点击下载对应版本的dilb链接
https://pypi.org/project/selenium/#description

方法三,编译安装

如果上述方法都不能解决问题,你可以尝试从源代码编译安装selenium。具体步骤可以参考selenium的官方文档。

Selenium 简介

Selenium 通过使用 WebDriver 支持市场上所有主流浏览器的自动化。 Webdriver 是一个 API 和协议,它定义了一个语言中立的接口,用于控制 web 浏览器的行为。 每个浏览器都有一个特定的 WebDriver 实现,称为驱动程序。 驱动程序是负责委派给浏览器的组件,并处理与 Selenium 和浏览器之间的通信。

这种分离是有意识地努力让浏览器供应商为其浏览器的实现负责的一部分。 Selenium 在可能的情况下使用这些第三方驱动程序, 但是在这些驱动程序不存在的情况下,它也提供了由项目自己维护的驱动程序。

Selenium 框架通过一个面向用户的界面将所有这些部分连接在一起, 该界面允许透明地使用不同的浏览器后端, 从而实现跨浏览器和跨平台自动化。

Selenium setup is quite different from the setup of other commercial tools. Before you can start writing Selenium code, you have to install the language bindings libraries for your language of choice, the browser you want to use, and the driver for that browser.

Follow the links below to get up and going with Selenium WebDriver.

If you wish to start with a low-code/record and playback tool, please check Selenium IDE

Once you get things working, if you want to scale up your tests, check out the Selenium Grid.

官方下载地址:https://www.selenium.dev/
官方pip:https://pypi.org/search/?q=selenium



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

你可能感兴趣的:(#,Python,Bug,selenium,测试工具,python,自动化)