1、首先安装好Python,这里采用当前官方最新版本3.9.7
来源https://www.python.org/
2、Python3.9.7安装完成以后,在软件安装提示的末尾,勾选添加path选项,这一步如果没有勾选的话,那么就手动添加
右键电脑属性,打开高级系统设置
点击环境变量
选中path,点击编辑
点击新建,将刚安装的Python目录路径复制过来
完成之后就可以在cmd中输入Python进入交互模式
3、在Windows中安装pip功能,pip安装采用官方给出的方式
pip官方来源https://pip.pypa.io/en/stable/installation/
4、cmd窗口下输入命令
输入pip3 install selenium
注意:这里为pip3,若是提示pip3模块不存在,检查自己是否是在Python中输入的。如果不是,那么将pip目录路径添加到Path中(图例为在Python中输入命令)
或是用dos命令直接进入到pip的目录下,直接输入pip3 install xxxx,就不用添加路径
5、检查selenium是否安装成功
命令:pip3 show selenium
6、安装selenium webdriver驱动,在官网之中选择自己对应的浏览器驱动下载,建议放在Python的Scripts目录下,就不用额外添加路径
Chrome: http://chromedriver.storage.googleapis.com/index.html
火狐:https://github.com/mozilla/geckodriver/releases/
IE:http://selenium-release.storage.googleapis.com/index.html
选择自己浏览器对应版本的驱动,建议都为最新。
7、执行验证
#!UTF-8
import unittest
from selenium import webdriver
class pys(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
print("加载驱动")
self.driver.implicitly_wait(3)
print("加载等待时长")
def test(self):
self.driver.get("https://www.baidu.com")
print("打开成功")
if __name__ == '__main__':
unittest.main()
print("执行完成")