解决: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

今天的分享就到此结束了

欢迎点赞评论关注三连

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: