解决:ModuleNotFoundError: No module named ‘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'



报错翻译

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


  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



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

  • 28
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ninghes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值