python+selenium自动化测试-Windows环境搭建
2021-08-11 本文已影响0人
Snails_Tin
来自python+selenium自动化测试初学者的笔记,写的不对的地方大家多多指教哦
一、安装selenium
安装selenium需要先安装python环境,python环境在之前的python+appium自动化测试-Android端环境配置中已经介绍过了,具体链接:https://www.jianshu.com/p/b665e00a8ea2
使用pip安装
pip install selenium
查看是否安装成功:
pip show selenium

二、下载安装Chromedriver
1.下载Chromedriver
注意:Chromedriver的版本一定要与Chrome的版本一致,Chrome的版本号可以打开浏览器-设置-关于Chrome查看;或者在浏览器中输入:chrome://version/(具体的)
下载地址:

例如:我的版本号是91.0.4472.124,所以我下载的Chromedriver版本号是:91.0.4472.101
2.配置
下载Chromedriver后解压缩,得到chromedriver.exe文件,将该文件复制到Chrome的安装目录下

右键点击我的电脑-属性-高级系统设置-环境变量-系统变量,将chromedriver.exe文件的路径添加到path中(即将C:\Program Files (x86)\Google\Chrome\Application添加到path中)
完成后cmd输入
chromedriver
查看是否配置成功,下图所示即配置成功

三、测试
编写一个简单示例,打开百度
from selenium import webdriver
import time
def main():
b = webdriver.Chrome()
b.get('<https://www.baidu.com>')
time.sleep(2)
b.quit()
if __name__ == '__main__':
main()
注意:在运行测试示例过程中出现这个错误:
'chromedriver' executable needs to be in PATH
解决措施:将chromedriver.exe文件拷贝到python目录下,就可以解决该问题
