Python3+Selenium3自动化测试-(准备)

Python3+Selenium3自动化测试-(准备)

最近在学习selenium自动化测试相关的内容,因此将实际准备状况作一记录,python

# 系统:win10(64位) # 浏览器:Chrome(67.0)、Firefox(61.0)、IE # python版本:3.6.5 # Selenium:3.13.0

Selenium简介

Selenium是一款适用于Web应用程序的便携式软件测试框架。 Selenium为编写测试提供了一个回放工具,无需学习测试脚本语言。它还提供了一种测试领域特定的语言,用于编写包括C#,Groovy,Java,Perl,PHP,Python,Ruby和Scala等多种流行编程语言的测试。git

Selenium官网地址:https://www.seleniumhq.org/github

Python安装

python官网地址:https://www.python.org/web

从python官网下载对应版本的python安装包,正常安装,须要注意的是须要将python加入环境变量中,可在安装的界面选择将Python加入环境变量中。chrome

环境变量设置:编程

个人电脑>>属性>>高级系统设置>>环境变量中编辑用户变量和系统变量api

Selenium的安装与测试

Selenium安装

使用python可直接利用pip进行安装selenium浏览器

启动cmd,注意:须要以管理员身份运行框架

pip install -U selenium

浏览器驱动driver安装

浏览器驱动driver的下载

driver的下载应该是比较坑的部分,必定须要注意浏览器版本。编程语言

selenium官网进入下载界面,这个时候请往下拉,虽然第三方的浏览器都不是selenium官方开发的,可是你能够在selenium官网找到selenium支持的浏览器相对应的驱动driver下载连接,由于我在本地使用的浏览器版本都是比较新的,因此对应的driver版本也都下载最新版便可。

这里仍是粘上三大浏览器的下载连接:

Google Chrome driver:https://sites.google.com/a/chromium.org/chromedriver/downloads

Mozilla GeckoDriver:https://github.com/mozilla/geckodriver/releases

Internet Explorer Driver:http://selenium-release.storage.googleapis.com/3.13/IEDriverServer_x64_3.13.0.zip

浏览器驱动driver的安装

下载下来的zip文件解压至python安装目录中,能够放置在:C:\Users\Administrator\AppData\Local\Programs\Python\Python36中,可是推荐放置在scripts目录中:C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts

 完成以上工做,咱们就能够进行测试使用selenium驱动浏览器了

测试驱动浏览器

CMD中启动python并从selenium引入webdriver包

from selenium import webdriver

驱动chrome浏览器

Ch_driver = webdriver.Chrome()
Ch_driver.get("https://www.google.com")
Ch_driver.quit() # 使用quit()关闭了chrome并结束了这次测试,若是是close()只是关闭chrome,后台仍在进行。

驱动Firefox浏览器

Fi_driver = webdriver.Firefox() Fi_driver.get("https://www.google.com") Fi_driver.quit()

驱动IE浏览器

Ie_driver = webdriver.Ie() Ie_driver.get("https://www.google.com") Ie_driver.quit()

 

看起来都正常驱动浏览器并打开了网页,这样咱们就完成了selenium自动化测试的准备工做~

相关文章
相关标签/搜索