安装Selenium客户端库报错

99 阅读1分钟

1、打开打开命令行程序,运行如下命令:pip install selenium,报错:ERROR: Could not install packages due to an OSError: HTTPSConnectionPool。 image.png

原因:网络不行。
解决方法:需要用科学上网的方法,然后重新运行上述命令,就可以成功安装Selenium客户端库。 image.png

2、在pycharm中运行以下代码时,报错:

image.png

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

wd = webdriver.Chrome(service=Service(r'D:\Runsoftware\tools\chromedriver.exe'))

wd.get('https://www.baidu.com')
input('等待回车键结束程序')

原因:

解决方法: 在File-Settings-项目文件夹-Project Structure下面点击Add Content Root,加入Selenium客户端库的安装路径:D:\Runsoftware\Python\Python312\Lib\site-packages,
pip show selenium来查询Selenium客户端库的安装路径: image.png

image.png 添加完毕之后,上述代码就可以运行成功,可以正常打开百度网页。